CakePHP Model-> invalidate does not show error

I am currently working on some additional form validation in the model's beforeValidate() .

I have the following code:

 function beforeValidate(){ $i = 0; foreach($this->data['CapitalCategory'] as $capital_category){ if(!empty($capital_category['value'])){ $this->invalidate('CapitalCategory.'.$i.'.points', 'error!'); return false; } $i++; } return true; } 

I debugged everything and it returns false if value is present. But then the form reloads, and no messages are displayed below the entered points! Also, if I debug validationErrors , the array contains an error that should be displayed. What could be the problem?

Appreciate any help!

EDIT

This is how I build my inputs:

 echo $this->Form->input('CapitalCategory.'.$i.'.value', array('label' => $category['Category']['name'], 'type' => 'text')); echo $this->Form->input('CapitalCategory.'.$i.'.points', array('label' => 'Puncte', 'type' => 'text')); 

I believe that the problem may be that I am working on a CapitalModel in which, in addition to some fields of CapitalModel, I used several fields from my sister model CapitalCategorieModel. Could this be a problem for not associating a validation error with a field? If so, how can I solve it?

+8
validation cakephp
source share
3 answers

Well, admittedly, I don't know why this is not showing you. From what I understand, this should be a "magic" trick. But I had things like this happening to me when you try to check and cakephp doesn't magically display the error. I solved this using another function of the FormHelper class.

 $this->Form->error( 'field' ); 

And again, I fully admit that this does not directly answer your question, but it is at least a cake that can cope with what is happening. Oh, and the function above returns null if there is no error, so you can just place it wherever you want the message to appear.

Here is a link to the function in the API if you want to look. FormHelper API 2.4

+1
source share

Perhaps this is due to the fact that CakePHP does not know where to put the error message

when you call $this->invalidate ( $name );
$name must match the name of the field (input) you created.

So can you show me how you create form inputs (fields)?

0
source share

Make sure you are not confusing the model :: $ validationErrors and Controller :: $ validationError. They are different and do not have a magical connection. One of them is filled with model methods, the other with controller methods.

0
source share

All Articles