【问题标题】:Two custom validation rules for same two fields not working when both are present in CakePHP 2当两个字段都存在于 CakePHP 2 中时,两个自定义验证规则不起作用
【发布时间】:2017-03-02 15:25:41
【问题描述】:

我的模型中有一个 $validate 数组,其中包含两个自定义验证规则。在我的“添加”和“编辑”操作中,如果用户未提供需要日期或未选中标有“不需要”的框,则应用程序应发出警告。如果用户同时提供了需要日期并选中了该框,则应用应发出警告,说明他们必须选择其中一个。

我的问题是,如果它们都存在,这些规则根本不起作用。如果我将其中一个注释掉,未注释的规则就可以正常工作。

这是模型中的代码:

public $validate = array(
'need_date' => array(
        'allowEmpty' => true,
        'rule' => 'checkNeedDate',
        'message' => 'Please enter a Need Date or check the box if not required.'
    ),
    'no_need_date' => array(
        'allowEmpty' => true,
        'rule' => 'checkNeedDate',
        'message' => 'Please enter a Need Date or check the box if not required.'
    ), 
    'need_date' => array(
        'allowEmpty' => true,
        'rule' => 'oneOrTheOther',
        'message' => 'Please enter EITHER \'Not Required\' OR a Need Date.'
    ),
    'no_need_date' => array(
        'allowEmpty' => true,
        'rule' => 'oneOrTheOther',
        'message' => 'Please enter EITHER \'Not Required\' OR a Need Date.'
    )
);

以下是来自同一模型的验证函数:

//Make sure that either the Requested Need Date field is filled in OR that the Not Required checkbox has been checked
function checkNeedDate($field) {
    if(!empty($this->data[$this->alias]['need_date']) || !empty($this->data[$this->alias]['no_need_date'])) {
        return true;
    } else {
        return false;
    }
}

//Make sure that the user has not filled in the Need Date field AND checked the Not Required box
function oneOrTheOther($field) {
    if(!empty($this->data[$this->alias]['need_date']) && !empty($this->data[$this->alias]['no_need_date']) ) {
        return false;
    } else {
        return true;
    }
}

我在这里可能做错了什么?

编辑:当存在 checkNeedDate() 时,oneOrTheOther() 函数在我的“添加”和“编辑”操作中都有效。 checkNeedDate() 似乎不是问题本身。

【问题讨论】:

    标签: php validation cakephp cakephp-2.x


    【解决方案1】:

    TLDR:检查"Multiple Rules per Field" area in the CakePHP 2 Book。 (您正在使用多个相同键覆盖第一个值)。


    更多详情:

    这是一个标准的array() 问题,也是经常被忽视的小问题之一(别担心,我们都至少做过一次)。

    如果您设置了一个已设置的键,它将覆盖之前的键值。

    例子:

    $food = [
        'pizza' => 'yuck',
        'vegetables' => 'meh',
        'pizza' => 'OMGYES!',
    ];
    

    结果数组将是:

    $food = [
        'pizza' => 'OMGYES!',
        'vegetables' => 'meh',
    ];
    

    试试这个:

    public $validate = array(
        'need_date' => [
            'customCheck' => [
                'allowEmpty' => true,
                'rule' => 'checkNeedDate',
                'message' => 'Please enter a Need Date or check the box if not required.'
            ],
            'needDate' => [
                'allowEmpty' => true,
                'rule' => 'oneOrTheOther',
                'message' => 'Please enter EITHER \'Not Required\' OR a Need Date.'
            ]
        ],
        'no_need_date' => [
            'checkNeedDate' => [
                'allowEmpty' => true,
                'rule' => 'checkNeedDate',
                'message' => 'Please enter a Need Date or check the box if not required.'
            ],
            'oneOrTheOther' => [
                'allowEmpty' => true,
                'rule' => 'oneOrTheOther',
                'message' => 'Please enter EITHER \'Not Required\' OR a Need Date.'
            ]
        ],
    );
    

    请注意,每个规则都在它自己的、唯一命名的相同字段名称键中。

    【讨论】:

    • 不错的解决方案。谢谢你。现在很有意义。
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多