【发布时间】: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