W
W
walder2019-01-28 12:08:58
Yii
walder, 2019-01-28 12:08:58

How to validate at least one field?

Hello.
There are two tables, the first table is "Categories", where there are id, cat_1, cat_2, cat_3, and the second is "Details", where id_cat, count, option.
The tables are connected and for each category I create a quantity and a property through a cycle, now everything works and everything is created, the question is how to do validation for at least one field, that is, if there is no quantity or property for all categories, then validate and show a message: " at least one category must not be empty", and if the property and quantity are specified for at least one category, then we save.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-01-28
@webinar

['cat1', 'required', 'when' => function($model) {
        return (empty($model->cat2) and empty($model->cat3));
    }]

https://www.yiiframework.com/doc/guide/2.0/en/inpu...
PS: structure id, cat_1, cat_2, cat_3 looks like a crutch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question