S
S
SlimSavernake2017-03-18 06:30:20
Yii
SlimSavernake, 2017-03-18 06:30:20

How to make a rule for checking the value of a field depending on the value of another field?

There are tables of settlements and streets connected through "locality_id"
In the ad there is a field for choosing a settlement and a field for choosing a street. It is necessary to check that the street is included in the settlement that was selected in the field above.
The code below only works on edit and I need validation on add as well.
Tell me how to do it right?

return [
['locality_id', 'in', 'range' => [1,2,3,4,5,6,7,8]],
['street_id', 'in', 'range' => Street::find()->select('id')->where(['locality_id' => $this->locality_id])->asArray()->column(), 'message' => 'Улица из другого города.'],
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-18
@SlimSavernake

Good morning.
Something you have done too much.
It is enough to check that id is a number, and which locality the street belongs to is no longer a validation concern.
At you as I understood, two tables.
When creating an entry, you make two interconnected drop-down lists.
In the first one, select a city, as soon as the contents of the first list change, send an ajax request to the server with the id of the locality and select streets from the second table where locality_id is equal to the id of the locality.
If no such records are found, return a message that nothing was found.
Leave validation alone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question