我想为Yii模型添加一些额外的验证。
我知道在一些逻辑中添加一个表单字段必须是6个字符的精确长度是足够简单的,但是是否有可能设置规则说以下内容:
if ($_POST['code'] == '')
then no validation needed
else
code string must be exactly 6 characters in length
在您的模型中添加此规则,如果code
是一个属性
public function rules()
{
return array(
array('code', 'length', 'is'=>6, 'allowEmpty'=>true),
);
}
查看更多信息,长度验证器
通过这个,你会得到你的答案,Yii验证器
您可以使用:
public function rules() {
return array(
array('code', 'length', 'is' => 6, 'allowEmpty' => true)
);
}