2

Cake Php Validation能否清除输入字段值

var $validate = array(
    'name' => array(
       'isUnique' => array (

           'rule' => 'isUnique',

           'message' => 'This Person name already exists.'
       )
    )
);

如果验证中仍然存在错误,我想清除name字段值。是否可以通过 cake php 验证本身来做到这一点?

4

1 回答 1

0

如果需要,您可以使用自定义验证规则来完成。

var $validate = array(
    'name' => array(
       'isUnique' => array (
           'rule' => 'ifNotUniqueClear', // use custom rule defined below
           'message' => 'This Person name already exists.'
       )
    )
);

function ifNotUniqueClear(&$data) {
    $field = key($data);

    // see if the record exists
    $user = $this->find('first', array(
        'conditions' => array(
            $field => $data[$field]
        ),
        'recursive' => -1
    ));

    if ($user) {
        // unset or empty it, your choice
        unset($this->data[$this->alias][$field]);
        return false;
    }

    return true;
}
于 2012-05-02T14:39:58.400 回答