Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: Either one field is required Validation

I have to implement the validation as mentioned in the title that either one of the two fields (email, phone) is required. I am doing this in my model:

[['email'],'either', ['other' => ['phone']]],

And this is the method:

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

When I access my index page, it gives me this error:

Exception (Unknown Property) 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\validators\InlineValidator::0'

Any help?

like image 246
Saani Avatar asked Jan 10 '17 10:01

Saani


3 Answers

If you don't care that both fields show an error when the user provides neither of both fields:

This solutions is shorter than the other answers and does not require a new validator type/class:

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

If you want to have a customized error message, just set the message option:

$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); }
  ],
];
like image 104
mplattner Avatar answered Oct 24 '22 01:10

mplattner


The rule should be:

['email', 'either', 'params' => ['other' => 'phone']],

And method:

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."));
    }
}
like image 10
Bizley Avatar answered Oct 24 '22 02:10

Bizley


Improved variant

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

Added 'skipOnEmpty'=>false for forcing validating and 'other' can be array

/**
 * 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),
    ]));

}
like image 1
uldis Avatar answered Oct 24 '22 02:10

uldis