1
votes

What is the simplest way to display validation errors for hasMany associations using Model::saveAssociated() in CakePHP 2.x?

Here is an example from the CakePHP cook book. (http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto)

echo $this->Form->create('Company', array('action' => 'add'));
echo $this->Form->input('Company.name', array('label' => 'Company name'));
echo $this->Form->input('Company.description');
echo $this->Form->input('Company.location');

echo $this->Form->input('Account.0.name', array('label' => 'Account name'));
echo $this->Form->input('Account.0.username');
echo $this->Form->input('Account.0.email');

echo $this->Form->end('Add');

When validation, for example, for Account.0.email fails the form field doesn't appear to be automatically invalidated. Although the error is listed in $this->Company->Account->validationErrors.

Is there a way to automatically invalidate the appropriate field and display the corresponding validation error?

1
Actually, this appears to be working as expected. I must have had a type in my Form inputs. When using the correct structure and naming conventions the associated validation errors do appear for the appropriate fields. +1 for CakePHP on this one!gunner1095
It would be good if you share more on what typo you have made. Perhaps even as your own answer. That way, other people will benefit from your experience. If my answer helped, do upvote it or even mark as correct if one part of it was the actual answerKim Stacks

1 Answers

0
votes

It is not 100% automated but there are things you can put together that CakePHP provides.

First of all, I am assuming you are using the latest 2.4

The solution I provide has 2 parts:

a) you validate the data at the controller level

b) you determine the error message at the FormHelper in the View level.

For part a)

you can reference this link http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html#validating-data-from-the-controller

Based on that link, I suggest 2 ways.

1) you can individually validate each model data.

Example,

if ($this->Company->validates(array('fieldList' => array('name', 'location')))) {
    // valid
} else {
    // invalid
}

and then you do a

if ($this->Account->validates(array('fieldList' => array('email')))) {
    // valid
} else {
    // invalid
}

Because you use a hasMany so there is a chance you may need to validate each different set of Account data separately.

Therefore you are likely to use a loop, so you can use and I quote,

if you are validating in a loop and want each set of errors separately don’t use invalidFields(). Instead use validates() and access the validationErrors model property.

2) second way is to use this

if ($this->Company->saveAll($this->request->data, array('validate' => 'only'))) {
  // validates
} else {
  // does not validate
}

You can find this at the bottom of the link I gave.

Now as for part b) displaying errors at the FormHelper.

You can read http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::error