validation if other fields not empty

2019-03-06 20:41发布

问题:

In the form, I have three fields: family, name and patronymic.

It is necessary to set up the validation in such a way that if at least one of them was filled, the others also became required. If not one is not completed, then the validation must be successful.

[
    ['family'],
    'required',
    'when' => function ($model) {
        return $model->name != null and $model->patronymic != null;
    },
],
[
    ['name'],
    'required',
    'when' => function ($model) {
        return $model->family != null and $model->patronymic != null;
    },
],
[
    ['patronymic'],
    'required',
    'when' => function ($model) {
        return $model->family != null and $model->name != null;
    },
],

回答1:

Update

What I suspect is the reason behind you are saying that it isn't working is because you are trying to achieve it on the frontend form or client side whereas you are using when in your current set of rules which does not give any idea if you are failing to do it at the frontend form, and it is'nt mentioned anywhere. Although it is working if you initialize the model manually and assign the values on the server side.

If that is correct you need to use the whenClient along with the when option for the rules.

See the updated rules below

return [

    [
        ['family'], 'required', 'when' => function ($model) {
            return $model->patronymic !== null || $model->name !== null;
        },
        'whenClient' => 'function(attribute,value){
            return $("#' . \yii\helpers\Html::getInputId($this, 'patronymic') . '").val()!=="" || $("#' . \yii\helpers\Html::getInputId($this, 'name') . '").val() !=="";
        }',
    ],
    [
        ['patronymic'], 'required', 'when' => function ($model) {
            return $model->family !== null || $model->name !== null;
        },
        'whenClient' => 'function(attribue,value){
            return $("#' . \yii\helpers\Html::getInputId($this, 'family') . '").val()!=="" || $("#' . \yii\helpers\Html::getInputId($this, 'name') . '").val() !=="";
        }',
    ],
    [
        ['name'], 'required', 'when' => function ($model) {
            return $model->patronymic !== null || $model->family !== null;
        },
        'whenClient' => 'function(attribute,value){
            return $("#' . \yii\helpers\Html::getInputId($this, 'patronymic') . '").val()!=="" || $("#' . \yii\helpers\Html::getInputId($this, 'name') . '").val() !=="";
        }',
    ],
];

You require "if one of the fields is filled, then the rest are required." change the conditions to OR instead of AND for example return $model->name != null and $model->patronymic != null; should be return $model->name != null OR $model->patronymic != null;, currently you are checking if both are not null then the field is required , which is inverse of what you want.

After changing your rules should look like below

[
    ['family'],
    'required',
    'when' => function ($model) {
        return $model->name != null || $model->patronymic != null;
    },
],
[
    ['name'],
    'required',
    'when' => function ($model) {
        return $model->family != null || $model->patronymic != null;
    },
],
[
    ['patronymic'],
    'required',
    'when' => function ($model) {
        return $model->family != null || $model->name != null;
    },
],