【问题标题】:CakePHP Validation nonBlank fails Whether the Field is Empty or !EmptyCakePHP Validation nonBlank 失败无论字段是空还是 !Empty
【发布时间】:2015-09-14 05:26:23
【问题描述】:

文档说'notBlank' 是您要确保它们不为空的字段的验证规则,如!empty($somevalue),但是当我将字段留空('')或输入值时在('s0meCraZyPasSworD')字段中仍然显示错误消息?

谁能看到我做错了什么?其余的验证工作类似于 minlength,但我将它们注释掉以更好地了解为什么 'notBlank' 似乎不起作用...

CONTROLLER:
// Set of validation rules to be run
$validateRules = [
    'fieldList' => [
        'currentpassword',
        'newpassword',
        'confirmpassword'
    ]
];

if ($this->Admin->validates( $validateRules )) {
    ...
}

MODEL:
class Admin extends AppModel
{
    public $name = 'Admin';

    public $validate = [
        'currentpassword' => [
            'notBlank'  => [
                'rule'    => 'notBlank',
                'message' => 'Current password is required.'
            ]
        ],
    ...

【问题讨论】:

    标签: validation cakephp cakephp-2.4


    【解决方案1】:

    你标记 CakePHP 2.4 - notBlankadded in 2.7 所以你必须使用 notEmpty 或设置 allowEmpty to false...

    【讨论】:

      【解决方案2】:

      发送到模型的 save() 方法的数据必须包含用于 登录字段。如果没有,验证将失败。默认值 因为这个键是 boolean false。

      required => true 和验证规则的意思不一样 非空白()。 required => true 表示数组键必须是 存在 - 这并不意味着它必须具有价值

      来自官网: http://book.cakephp.org/2.0/en/models/data-validation.html#required

      因此,您将需要另一个规则,例如 allowEmpty 来验证此字段,而不是 notBlank 规则。

      【讨论】:

        猜你喜欢
        • 2020-04-06
        • 2015-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多