Give this site a read over. This is where I found a good example that worked
for me validating HABTM

http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/

Dave 

-----Original Message-----
From: mcrawford [mailto:[email protected]] 
Sent: August-10-09 4:03 PM
To: CakePHP
Subject: Validating a HABTM relationship


Hi,

I have a model set up like this:

class Question extends AppModel {
        var $name = 'Question';
        var $hasAndBelongsToMany = array(
                'FunctionalArea' => array('joinTable' =>
'question_functions')
        );
        var $validate = array(
                'title' => 'notEmpty',
                'content' => 'notEmpty',
                'FunctionalArea' => array(
                        'rule' => array('multiple', array('min'=>1)),
                        'required' => true
                )
        );
}

Then in my form I set up an input with FunctionalArea as the field name,
using Cake's form helper. This produces an input with name="data
[FunctionalArea][FunctionalArea][]". Now if I save the form with NO
validation, the vanilla $this->Question->save($this->data); works just fine.
The join table is all correct after saving.

But when I put in that validation for FunctionalArea that I have in the
above code, the validation doesn't work correctly. The model's invalidFields
method does this:
if (isset($data[$this->alias])) {
        $data = $data[$this->alias];
}
So that the data in data[FunctionalArea][FunctionalArea] never gets to the
validate method. So the validation always fails (because FunctionalArea is
required).

My question is: what is the correct Cake way to do this? I'm trying to use
the automatic save feature, form fields, and the validation, but I don't see
how I can use them together, because the validation seems to be ignoring the
very place where the save needs the data.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to