-1

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;
    },
],
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
triest23
  • 1
  • 4

1 Answers1

2

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;
    },
],
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • Not working. Validate success only if all filds not null. – triest23 Jan 25 '19 at 10:09
  • @triest23 you have mentioned that validation will be raised **only if any of the 3 fields is filled and the rest 2 are not** then it should ask you to fill in the rest of the 2 fields. otherwise, if none of the fields is filled then it should allow the validation to pass. is that correct? if not please update your question with the exact requirements the above solution works correctly, the only thing i suspect is that you are trying to check it on client side not server side, if that is the case please describe. – Muhammad Omer Aslam Jan 25 '19 at 11:45
  • In this form, it also does not pass validation, if no field is filled. https://github.com/triest/Insigne/blob/master/backend/models/EditForm.php – triest23 Jan 25 '19 at 12:57
  • i have updated the answer if you see above, you havent updated the rules accordingly, update them and it will work correctly, and use `!==null` rather than `!=null` use the code i added in the update part of the answer. – Muhammad Omer Aslam Jan 25 '19 at 12:59