Can Cake Php验证可以清除输入字段值

时间:2012-05-02 10:50:53

标签: cakephp cakephp-1.3

Cake Cake Php验证可以清除输入字段值

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

           'rule' => 'isUnique',

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

如果验证中仍存在错误,我想清除name字段值。蛋糕php验证本身可以吗?

1 个答案:

答案 0 :(得分: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;
}