Can Cake Php Validation clear input field value

落爺英雄遲暮 提交于 2019-12-22 21:44:46

问题


Can Cake Php Validation clear input field value

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

           'rule' => 'isUnique',

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

If error persist in validation, I want to clear name field value. Is it possible to do so with cake php validation itself ?


回答1:


You can do it with a custom validation rule if you wanted.

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;
}


来源:https://stackoverflow.com/questions/10412257/can-cake-php-validation-clear-input-field-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!