当收到一个值时,我需要在模型上制定一个验证规则——应用一个规则,当应用另一个规则时,再制定一个值。
例如:
if($model->id = "1")
{
return array(
array('numero_documento', 'length'),
);
}
if($model->id = "2")
{
return array(
array('numero_documento', 'numerical', 'integerOnly'=>true),
);
}
这是我想在模型规则
您可以重构像这样的规则方法
public function rules()
{
$rules = array(//index of your habitual rules);
if ($this->id == 1) {
$rules[] = array('numero_documento', 'length');
} elseif ($this->id == 2) {
$rules[] = array('numero_documento', 'numerical', 'integerOnly'=>true);
}
return $rules
}
然而,这是一种罕见的验证规则设计