D
D
Denis Popov2018-12-04 05:56:42
Yii
Denis Popov, 2018-12-04 05:56:42

How to do array validation in Active Form Yii2?

Is it possible to somehow validate an array of models, or how to say it correctly, in general, a contact form, fields are added dynamically.

<div>
<select name="type[0]">
  <option value="ok">Одноклассники</option>
  ...
  <option value="vk">ВК</option>
</select>
<input type="text" name="value[0]" />
</div>
...
<div>
<select name="type[0]">
  <option value="ok">Одноклассники</option>
  ...
  <option value="vk">ВК</option>
</select>
<input type="text" name="value[0]" />
</div>

Something like this. There is only each, which is clearly not relevant here, since it is used when the model has one of the fields - an array, and here, in fact, an array of models

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-12-04
@DenisDangerous

This is the so-called tabular input .
And your form is incorrect, you will get only one value type[0] and value[0].

<div>
<select name="social[0][type]">
  <option value="ok">Одноклассники</option>
  ...
  <option value="vk">ВК</option>
</select>
<input type="text" name="social[0][value]" />
</div>
...
<div>
<select name="social[1][type]">
  <option value="ok">Одноклассники</option>
  ...
  <option value="vk">ВК</option>
</select>
<input type="text" name="social[1][value]" />
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question