I
I
IdFox2016-06-15 05:48:19
Yii
IdFox, 2016-06-15 05:48:19

How to validate dynamically added form fields in YII2?

Hello everyone, the subject
itself There is ActiveForm, there is a form validation class
In the validation class all known form attributes and rules for them are registered
And how to make validation of dynamically added form fields?
Well, that is, to do so, so to speak, validation not by the field (name), but, so to speak, by some attribute of it (I don’t know the class) ...
That is, for example, there is one tariff in the form (price list position)
It consists of ID , names and prices
I can register in the validator (for example)

.......

[
    ['id', 'name', 'price'],
    'required',
    'message' => 'Укажите значение',
],

[
    ['price'],
    'integer',
    'message' => 'Укажите целое число',
],

[
    ['name'],
    'string',
    'max' => 250,
],

.......

But what if, for example, a price list is filled with several elements
. That is, I will have an input with id2, id3, id4 (name1, name2, name3) in the form, and so on
. I can’t say in advance how many there will be.
How can I validate these fields on the server and on the client?
Yes, you can wrap the whole thing in PJAX and validate everything on the server, but I would like a native implementation.
That is, set something like a validation template class and, for example, write in the example above
.......

[
    ['patern_id', 'patern_name', 'patern_price'],
    'required',
    'message' => 'Укажите значение',
],

[
    ['patern_price'],
    'integer',
    'message' => 'Укажите целое число',
],

[
    ['patern_name'],
    'string',
    'max' => 250,
],
.......

Thanks everyone for the replies

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-06-15
@IdFox

"I do not read the documentation, I write as I want."?
www.yiiframework.com/doc-2.0/guide-input-tabular-i...

M
Maxim Fedorov, 2016-06-15
@qonand

Use a dynamic model , it will solve your problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question