0
votes

I have the following problem:
I need to validate data in a controller, which is passed by a form. The special thing: The fields from the form are not present in the database, BUT in the $validate-Array. - Which should afaik not be a problem.
Another thing is, that the Model has a hasMany-Relationship with parts of this form.

Text hasMany Proofs

This is the validation-array:

var $validate = array(  
   'freetext' => array(  
      'between' => array(  
          'rule' => array('between', 250, 1000),  
          'message' => '..',  
      ),  
   ),  
);  

Here is an example of the data passed to the controller

[Proof] => Array
    (
        [0] => Array
            (
                [freetext] => asd
            )
        [1] => Array
            (
                [freetext] => asd
            )
        [2] => Array
            (
                [freetext] => asd
            )
    )

I use the following code to validate:

$this->Texts->set($this->data);   
if ($this->Texts->validates()) {  

When I call it, it returns true, so the validation succeed. BUT freetext has a bit less than 250 chars.

But: The form shows the red asterisks, which symbolizes me, that the validation-array is succesfully parsed an applied to the form.
And also in the corresponding controller beforeValidate() { debug($this->data); } shows me the correct data.

So WHY does data validation succeed, even when the policies are not me?

Thanks ahead!
~MxAgent

2
I believe your Model calls should be singular, not plural - "Text", not "Texts". Also, isn't 'beforeValidate()' a model method, not a controller action? - Dave
This is just because I changed some names due to sensible data.. - Micronax
Is it a problem, that the field is not existing in the table but in the validation-array and in the form. I need to process the data, the user entered and then write it partly into another fields.. - Micronax
foreach($this->data['Proof'] as $proof) { $proof = array('Proof' => $proof); $this->Proof->set($proof); if (!$this->Proof->validates()) echo "err";} works! Validation fails, BUT no message is displayed to the corresponding field.. - Micronax

2 Answers

0
votes

A quick jump into the CookBook (http://book.cakephp.org/view/1154/between) reveals that your format seems to be incorrect. Try this(drop the "between" array):

var $validate = array(  
   'freetext' => array(  
          'rule' => array('between', 250, 1000),  
          'message' => '..',  
   )  
);  
0
votes

Updated to cakePHP 2.0 and used validateMany() for validation. Now it works ;)