【发布时间】:2017-05-24 19:24:19
【问题描述】:
我必须实现标题中提到的验证,即需要两个字段(电子邮件、电话)中的任何一个字段。我在我的model 中这样做:
[['email'],'either', ['other' => ['phone']]],
这就是方法:
public function either($attribute_name, $params) {
$field1 = $this->getAttributeLabel($attribute_name);
$field2 = $this->getAttributeLabel($params['other']);
if (empty($this->$attribute_name) && empty($this->$params['other'])) {
$this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
return false;
}
return true;
}
当我访问我的索引页面时,它给了我这个错误:
异常(未知属性)'yii\base\UnknownPropertyException' message '设置未知属性:yii\validators\InlineValidator::0'
有什么帮助吗?
【问题讨论】:
-
参数不应该在数组中。
-
把参数改成不在数组中,还是一样的错误!
-
尝试在
empty($this->{$params['other']})中添加大括号 -
然后给出语法错误! @Bizley
标签: php validation yii active-form