Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 666
linkyndy Avatar asked Apr 06 '11 16:04

linkyndy


1 Answers

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

like image 152
GatorGuy023 Avatar answered Oct 22 '22 21:10

GatorGuy023