S
S
SokolovArtur2016-08-09 09:08:02
JavaScript
SokolovArtur, 2016-08-09 09:08:02

How to dynamically add validation rules in Yii2?

I am using Active Form. Some form fields are taken from the database. Their type, name and validation rules are stored there. The fields are different and have different characteristics. How to set validation rules for these fields? So that the framework understands them, both on the server side and on the client side.
Stayed to use DynamicModel . But the class needs to pass an attribute with the field value. Means approaches only for server validation. What about the client?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander N++, 2016-08-09
@sanchezzzhak

With client validation, you need to add js code
www.yiiframework.com/doc-2.0/guide-input-validatio...
in models, you can assign rules for scenarios

M
Maxim Timofeev, 2016-08-09
@webinar

use DynamicModel for this

$model = DynamicModel::validateData(compact('name', 'email'), [
        [['name', 'email'], 'string', 'max' => 128],
        ['email', 'email'],
    ]);

here are the docks:
www.yiiframework.com/doc-2.0/yii-base-dynamicmodel.html I
did this as a form generator (questionnaires), while the data about the fields and their settings are stored in the database in json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question