Found a solution:

As far as I can tell, the validation methods expect the data all to be
in (for example) $this->data['Question']['FunctionalArea'].

(See line 2196 of model.php, where the only data considered for
validation is in $this->data['Question'].)

But the save method expects all the data to be in $this->data
['FunctionalArea']['FunctionalArea'].

(See lines 1120-1125 of model.php, which considers everything in $this-
>data, including HABTM relationships.)

So I my View posts the data to $this->data['FunctionalArea']
['FunctionalArea'], and in the Controller I copy it to $this->data
['Question']['FunctionalArea'], so that both validation and saving
work.

On Aug 10, 5:24 pm, mcrawford <mcrawf...@gmail.com> wrote:
> Hmm, this is not quite right either. Now the associated models do not
> save...
> According tohttp://book.cakephp.org/view/85/Saving-Related-Model-Data-HABTM
> I should be using
> $form->input('FunctionalArea')
> but that doesn't work with the validation. But it does work with the
> save function.
>
> Dave, thanks, I did find that link when Googling for a solution. I was
> hoping to avoid writing my own validation method since the multiple
> one does exist, although it doesn't sound like people are having
> success using it.
>
> On Aug 10, 11:49 am, mcrawford <mcrawf...@gmail.com> wrote:
>
> > Ah, I found it in the Form helper...
>
> > $form->input('Question.FunctionalArea')
>
> > I need to use the full dot-separated name of the field to get the form
> > field to be what the validation is expecting.
>
> > On Aug 10, 11:32 am, mcrawford <mcrawf...@gmail.com> wrote:
>
> > > 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to