CakePHP Show Validation Message in Hidden Field

In my form, I created a hidden field:

echo $this->Form->hidden('editor_rating', array('value' => 0)); 

What outputs:

In my model, I created a validation rule:

 'editor_rating' => array( 'rule' => array('comparison', 'greater or equal', 1), 'message' => 'Please choose a valid Editor Rating' ) 

When I submit the form, an error class is added to the hidden field, but there are no visible changes and there is no error message:

 <input id="ListingEditorRating" class="form-error" type="hidden" value="0" name="data[Listing][editor_rating]"> 

How can I make this error message visible or even attach it to another div?

+4
source share
2 answers

FormHelper :: Error

In cases of using Form->input or Form->inputs you cannot explicitly display errors :

 if ($this->Form->isFieldError('gender')) { echo $this->Form->error('gender'); } 
+3
source

OK, so it doesn't seem like any inline method is processing what I need, which is understandable, so I process it manually, checking for validationErrors for this field.

Here is a cleaner example than the editor_rating field I used:

(artist_picker uses jQuery autocomplete to get a list of suitable artists. We want to display the artist name in the input file, but you need to send artist_id to the database, therefore, update the hidden field)

 echo $this->Form->hidden('artist_id', array('div' => false)); echo $this->Form->input('artist_picker', array( 'label'=> false, 'div'=> (isset($this->validationErrors['Listing']['artist_id']) ? 'span4 error' : 'span4'), // Turn on error class if errors 'class' => (isset($this->validationErrors['Listing']['artist_id']) ? 'span12 form-error' : 'span12'), // Turn on form-error class if errors 'after' => (isset($this->validationErrors['Listing']['artist_id']) ? '<div class="error-message">'.$this->validationErrors['Listing']['artist_id'][0].'</div>' : ''), 'type'=>'text') // Show error message if errors ); 
+1
source

Source: https://habr.com/ru/post/1491287/


All Articles