cba*_*cat 3 validation cakephp
我有一个用于注册和更新数据的表单。它工作正常,但是当我更新数据时,模型验证返回 false,因为插入的值已经存在。
我有以下代码:
/* 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;
}
Run Code Online (Sandbox Code Playgroud)
那么,如何禁用唯一规则,但保留其他规则?
您可以添加on验证规则并仅在create以下位置接受此验证:
public $validate = array(
    'unique' => array(
        'rule' => array('unique', 'field in the table'),
        'message' => 'The field cannot be empty.',
        'on' => 'create'
    );
);
Run Code Online (Sandbox Code Playgroud)