Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 validating array of inputs

Tags:

yii2

I have following data :

Array
(
    [category] => Array
        (
            [0] => d
            [1] => 100
            [2] => 100
            [3] => 100
        )

    [volume] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )

    [urgency] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )

    [importance] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )
)

And I created DynamicModel for it with rules "each value should be integer" (added in 2.0.4).

$view_model = DynamicModel::validateData(compact('category', 'volume', 'urgency', 'importance'), [
        [['category', 'volume', 'urgency', 'importance'], 'each', 'rule' => ['integer']],
    ]);

In view I have:

    <?= $form->field($model, 'category[0]')->textInput() ?>
    <?= $form->field($model, 'category[1]')->textInput() ?>
    <?= $form->field($model, 'category[2]')->textInput() ?>
    ...
    <?= $form->field($model, 'importance[2]')->textInput() ?>

Problem is, when I submit form with "d" in first input, I have errors on each "category" input: enter image description here

What I do wrong?

like image 592
Ilya Levin Avatar asked Oct 18 '15 13:10

Ilya Levin


People also ask

How does validation work in Yii?

Here's how Yii describes the flow of validation. Typically, you can use the default scenario and don't need to build your own. You'll generally need to rely on Gii to generate rules or write your own. When the validate () method is called, it goes through the following steps to perform validation:

Should I use the Yii framework?

The Yii Framework provides a ton of baseline validation features so there's no need to build them from scratch. But, if you need some custom extensions, that's straightforward as well. Validations are yet another reason why I think it always makes sense to build applications on a web framework such as Yii rather than vanilla PHP.

Which validation rules should be used to validate each attribute?

Determine which validation rules should be used by getting the rule list from yiibaseModel::rules () using the current scenario. These rules are called active rules. Use each active rule to validate each active attribute which is associated with the rule. The validation rules are evaluated in the order they are listed.

How to use Yii\base\dynamicmodel() method in a dynamic model?

Its usage is like the following: The yii\base\DynamicModel::validateData () method creates an instance of DynamicModel, defines the attributes using the given data ( name and email in this example), and then calls yii\base\Model::validate () with the given rules.


1 Answers

You can use Each validator Info: This validator has been available since version 2.0.4.

[
    // checks if every category ID is an integer
    ['categoryIDs', 'each', 'rule' => ['integer']],
]

This validator only works with an array attribute. It validates if every element of the array can be successfully validated by a specified validation rule. In the above example, the categoryIDs attribute must take an array value and each array element will be validated by the integer validation rule.

rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.
allowMessageFromRule: whether to use the error message returned by the embedded validation rule. Defaults to true. If false, it will use message as the error message.

Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message.
like image 112
Amitesh Kumar Avatar answered Oct 01 '22 22:10

Amitesh Kumar