以更新形式禁用验证 - CakePHP [英] Disable validation in update form - CakePHP

查看:96
本文介绍了以更新形式禁用验证 - CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我用来注册和更新数据。
它工作正常,但是当我更新数据,模型验证返回false,因为插入的值已经存在。



我有以下代码: / p>

  / *验证规则* / 
public $ validate = array(
'unique'=> array (
'rule'=> array('unique','在表中'字段),
'message'=>'该字段不能为空'

...

/ *检查值是否已经存在的函数* /
public function unique($ value,$ field){
$ check = $ this - > find('count',array(
'recursive'=> -1,
'conditions'=> array(
$ this-& > $ value

));

return $ check == 0;所以,我如何禁用唯一规则,但保留其他规则?

解决方案

您可以在验证规则中添加,并接受此验证只有创建

  public $ validate = array b'unique'=> array(
'rule'=> array('unique','表中的字段'),
'message'=>'该字段不能为空。 ',
'on'=>'create'
);
);



参考:




I have a form that i use to register and update data. It works fine, but when i update the data, the model validation returns false, because the value inserted already exists.

I have the following code:

/* Validation Rule*/
public $validate = array(
    'unique' => array(
        'rule' => array('unique', 'field in the table'),
        'message' => 'The field cannot be empty.'
    )
    ...
)
/* Function that check if the value already exists */
public function unique($value, $field) {
    $check = $this->find('count', array(
        'recursive' => -1,
        'conditions' => array(
            $this->Alias.$field => $value
        )
    ));

    return $check == 0;
}

So, how can i disable the unique rule, but keeping the other rules?

解决方案

You can add on in your validation rules and accept this validation only on create:

public $validate = array(
    'unique' => array(
        'rule' => array('unique', 'field in the table'),
        'message' => 'The field cannot be empty.',
        'on' => 'create'
    );
);

Reference:

这篇关于以更新形式禁用验证 - CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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