CakePHP 2 中同时存在两个相同字段的两个自定义验证规则不起作用
Two custom validation rules for same two fields not working when both are present in CakePHP 2
我在一个包含两个自定义验证规则的模型中有一个 $validate 数组。在我的 'add' 和 'edit' 操作中,如果用户未提供需要日期或未选中标有 "Not Required," 的框,则应用程序应发出警告。如果用户同时提供了需要日期并勾选了该框,应用程序应发出警告,说明他们必须选择其中一个。
我的问题是,如果同时存在这些规则,它们将根本不起作用。如果我将一个或另一个注释掉,则未注释的规则可以正常工作。
这是模型中的代码:
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() 函数在我的 'add' 和 'edit' 操作中都有效。当 checkNeedDate() 本身不是问题时,它似乎是问题所在。
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 字段名键中,它自己的、唯一命名的键。
我在一个包含两个自定义验证规则的模型中有一个 $validate 数组。在我的 'add' 和 'edit' 操作中,如果用户未提供需要日期或未选中标有 "Not Required," 的框,则应用程序应发出警告。如果用户同时提供了需要日期并勾选了该框,应用程序应发出警告,说明他们必须选择其中一个。
我的问题是,如果同时存在这些规则,它们将根本不起作用。如果我将一个或另一个注释掉,则未注释的规则可以正常工作。
这是模型中的代码:
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() 函数在我的 'add' 和 'edit' 操作中都有效。当 checkNeedDate() 本身不是问题时,它似乎是问题所在。
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 字段名键中,它自己的、唯一命名的键。