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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With