CakePHP Model->invalidate doesn't show error
I am currently working on some extra validation on a form in the beforeValidate()
callback of the model开发者_开发百科.
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 does return false if the value is present. But then, the form reloads and no message is shown below the points input! Also, if I debug the validationErrors
, the array contains the error that needs to be displayed.
What could be the problem?
Appreciate any help!
EDIT
This is the way I am building 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 a problem could be the fact that I am working on CapitalModel in which, besides some fields of the CapitalModel, I have used several fields from its related model, CapitalCategorieModel. Could this be the problem for not binding the validation error to the field? If yes, how can I solve it?
Well, admittedly I do not know why it is not showing for you. From what I understand it should be doing the cakephp 'magic' thing. But I have had things like this occur with me when trying to validate and cakephp not magically displaying the error. I solved this by using another function of the FormHelper class.
$this->Form->error( 'field' );
And again i do completely acknowledge this doesn't directly answer your question but this is at least a cake way of handling what is going on. Oh and the function above returns null if there is no error so you can just place it wherever you'd like the message to appear.
Here is the cake link to the function in the API if you want to look. FormHelper API 2.4
Probably this is because CakePHP doesn't know where to put the error message
when you call $this->invalidate ( $name );
$name
must match with the name of the field (input) you have created..
so, can you show me how you are creating the form inputs (fields) ?
Make sure you are not confusing model::$validationErrors and Controller::$validationError. They are different and don't share a magic bond. One is populated by model methods, the other populated by controller methods.
精彩评论