【问题标题】:Yii2: Either one field is required ValidationYii2:需要一个字段验证
【发布时间】: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


【解决方案1】:

规则应该是:

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

【讨论】:

  • 不应该 ||如果只有两个属性都为空时才应添加错误,则不是 &&?
【解决方案2】:

改进的变体

        ['gipsy_team_name', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => 'poker_strategy_nick_name']],
        ['vkontakte', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => ['odnoklasniki','odnoklasniki']]],

添加 'skipOnEmpty'=>false 用于强制验证,'other' 可以是数组

/**
 * validation rule
 * @param string $attribute_name
 * @param array $params
 */
public function either($attribute_name, $params)
{
    /**
     * validate actula attribute
     */
    if(!empty($this->$attribute_name)){
        return;
    }

    if(!is_array($params['other'])){
        $params['other'] = [$params['other']];
    }

    /**
     * validate other attributes
     */
    foreach($params['other'] as $field){
        if(!empty($this->$field)){
            return;
        }
    }

    /**
     * get attributes labels
     */
    $fieldsLabels = [$this->getAttributeLabel($attribute_name)];
    foreach($params['other'] as $field){
        $fieldsLabels[] = $this->getAttributeLabel($field);
    }

    $this->addError($attribute_name, \Yii::t('poker_reg', 'One of fields "{fieldList}" is required.',[
        'fieldList' => implode('"", "', $fieldsLabels),
    ]));

}

【讨论】:

    【解决方案3】:

    如果您不关心当用户提供两个字段都没有时这两个字段都显示错误:

    此解决方案比其他答案更短,并且不需要新的验证器类型/类:

    $rules = [
      ['email', 'required', 'when' => function($model) { return empty($model->phone); }],
      ['phone', 'required', 'when' => function($model) { return empty($model->email); }],
    ];
    

    如果您想要自定义错误消息,只需设置message 选项:

    $rules = [
      [
        'email', 'required',
        'message' => 'Either email or phone is required.',
        'when' => function($model) { return empty($model->phone); }
      ],
      [
        'phone', 'required',
        'message' => 'Either email or phone is required.',
        'when' => function($model) { return empty($model->email); }
      ],
    ];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 2011-07-21
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多