M
M
My joy2018-02-20 10:43:09
Yii
My joy, 2018-02-20 10:43:09

Why doesn't when in rule work?

Hi friends.
There is a rule in the settings form:

['value', 'integer', 'min' => '1', 'max' => '9', 'when' => function($m) { return $m->param == 'sku_max'; }],

those. literally - if the parameter is called sku_max then it is only the number 1..9 . But for some reason this rule works for any parameter name. Why doesn't when work ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
My joy, 2018-02-20
@t-alexashka

It turns out everything worked as it should on the PHP side. and here client validation ignores when, from here and did not start up further. the solution is to turn off client-side validation: in the form parameters.

M
Maxim Fedorov, 2018-02-20
@qonand

the problem is not in the rule, if this rule works for you, then really $m->param == 'sku_max' returns true. Here you need to debug the code, maybe you have param set to 'sku_max' by default, and for example you do not load a new parameter name into the model

O
OKyJIucT, 2018-02-20
@OKyJIucT

Instead of "value" you need to specify the name of your parameter "param", because you check it for equality to the value of 'sku_max'

['param', 'integer', 'min' => '1', 'max' => '9', 'when' => function($m) {
    return $m->param == 'sku_max'; 
}],

In general, it can be made easier
['param', 'in', 'range' => range(1, 9)],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question