Can Cake Php Validation clear input field value

99封情书 提交于 2019-12-06 09:34:44

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