可以通过Cake Php验证清除输入字段的值 [英] Can Cake Php Validation clear input field value

查看:156
本文介绍了可以通过Cake Php验证清除输入字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Can Cake Php Validation清除输入字段值

Can Cake Php Validation clear input field value

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

           'rule' => 'isUnique',

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

如果错误在验证中仍然存在,我想清除 name 字段值。有可能这样做用蛋糕php验证本身?

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

推荐答案

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

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

这篇关于可以通过Cake Php验证清除输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆