在CakePHP 2.x中为hasMany关联显示验证错误的最简单的方法是什么? [英] What is the simplest way to display validation errors for hasMany associations in CakePHP 2.x?

查看:97
本文介绍了在CakePHP 2.x中为hasMany关联显示验证错误的最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP 2.x中使用Model :: saveAssociated()显示hasMany关联的验证错误的最简单方法是什么?



这里是CakePHP厨师书。
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');

验证时,例如,Account.0.email失败,自动无效。虽然错误列在$ this-> Company-> Account-> validationErrors。



有没有办法自动使相应的字段无效并显示相应的验证错误?

解决方案

这不是100%自动化,但有一些你可以放在一起的CakePHP提供。



首先,我假设你使用的是最新的2.4



我提供的解决方案有两部分:



a)您在控制器级别验证数据



b)您在View级别的FormHelper确定错误消息。 / p>

对于a)



您可以参考此链接 http://book.cakephp.org /2.0/en/models/data-validation/validating-data-from-the-controller.html#validating-data-from-the-controller



根据该链接,我建议两种方式。



1)您可以单独验证每个模型数据。



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

,然后执行

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

因为你使用hasMany,所以有可能需要验证每个不同的帐户数据集。



因此,您可能使用循环,因此您可以使用和引用


如果你在一个循环中进行验证,并且想要单独的错误集合
不使用invalidFields()。而是使用validates()并访问
validationErrors模型属性。


2)第二种方法是使用

  if($ this-> Company-> saveAll($ this-> request-> data,array('validate' =>'only'))){
//验证
} else {
//不验证
}

你可以在我给的链接的底部找到这个。



)在FormHelper上显示错误。



您可以阅读 http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::error


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?

解决方案

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

这篇关于在CakePHP 2.x中为hasMany关联显示验证错误的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆