I'm using a tip I saw recently somewhere (Teknoid?) to validate HABTM on save:

$this->MainModel->OtherModel->set($this->data);

if (!$this->MainModel->OtherModel->validates())
{
        $this->MainModel->OtherModel->invalidate('OtherModel');
}
else if ($this->MainModel->save($this->data))
{
        ...

OtherModel is named 'Discipline' in this case. That model's $validate:

public $validate = array(
        'Discipline' => array(
                'rule' => 'checkExists',
                'message' => 'Please choose at least one discipline.'
        )
);

public function checkExists()
{
        return !empty($this->data['Discipline']['Discipline']);
}

So, this appears to be working, except that I can't figure out how to
display a message. All I get is "1" (number one).

I tried both $form->error('Discipline') and
$form->error('Discipline.Discipline') before I thought to dump the
$validationErrors in the view, which look like:

Array
(
    [Discipline] => Array
        (
            [Discipline] => 1
        )

)

The submitted data looks like this (empty form submission for testing):

Array
(
    [MainModel] => Array
        (
            // empty fields
        )

    [Discipline] => Array
        (
            [Discipline] =>
        )

)

The problem appears to be in Discipline->validate, because the rule
isn't on a field. Since Discipline is always a HABTM and only the ID
is passed, I changed it to:

public $validate = array(
        'id' => array(
                'rule' => 'checkExists',
                'message' => 'Please choose at least one discipline.'
        )
);

But, now, I don't get any error at all for Discipline. In fact,
checkExists doesn't even get used. I next tried:

public $validate = array(
        'Discipline.Discipline' => array(
                'rule' => 'checkExists',
                'message' => 'Please choose at least one discipline.'
        )
);

It still won't run. Cake doesn't seem to want to run the validation
rule unless I have just the single model name, and then won't pass the
error msg.

Ideas?

--~--~---------~--~----~------------~-------~--~----~
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