假设我有十几个单选按钮字段,我想根据两个共同的规则进行验证。
'valid'=> array(
'rule' => array('inList', array('yes','no')),
'message' => 'Illegal Choice Detected'
),
'required'=> array(
'rule' => array('notEmpty'),
'message' => 'Field is required.'
),
我如何才能做到这一点,而不必将每个验证规则分配给每个字段?
编辑
对于那些喜欢像我自己有时喜欢的勺子喂养的人来说,下面是我根据Burzum的回答所做的事情!
public function beforeValidate($options = []) {
$fields = ['field_1','field_2','field_3','etc'];
foreach ($fields as $field) {
$this->validate[$field]['required'] = array(
'rule' => array('notEmpty'),
'message' => 'Field is required.'
);
$this->validate[$field]['legal'] = array(
'rule' => array('inList', array('yes', 'no')),
'message' => 'An illegal choice has been detected, please contact the website administrator.'
);
}
return true;
}
发布于 2014-03-27 05:31:56
将它们添加到beforeValidate()中的foreach循环中
public function beforeValidate($options = []) {
$fields = ['field1', '...'];
foreach ($fields as $field) {
// Add your rule(s) here to the field
$this->validate[$field]['myRule'] = ['...'];
}
return true;
}
https://stackoverflow.com/questions/22688995
复制相似问题