微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Yii2:任何一个字段都需要验证

我必须实现标题中提到的验证,即需要两个字段之一(电子邮件,电话).我在我的模型中这样做:

[['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;
    }

当我访问我的索引页面时,它给了我这个错误

Exception (UnkNown Property) ‘yii\base\UnkNownPropertyException’ with
message ‘Setting unkNown property: yii\validators\InlineValidator::0’

有帮助吗?

最佳答案:

规则应该是:

['email', 'either', 'params' => ['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."));
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐