当CakePHP 2中存在两个字段时,两个相同的两个字段的自定义验证规则不起作用

时间:2017-03-02 15:25:41

标签: php validation cakephp cakephp-2.x

我在一个带有两个自定义验证规则的模型中有一个$ 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()似乎是它本身不存在的问题。

1 个答案:

答案 0 :(得分: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.'
        ]
    ],
);

请注意,每个规则都在SAME字段名称密钥中的唯一命名密钥中。