[[+content_image]]
R
R
Ruslan2020-11-30 08:56:18
Yii
Ruslan, 2020-11-30 08:56:18

Why doesn't the integer validation rule work?

There is a Siganl model that has organization and color
attributes The model has validation rules

[['organization', 'color'], 'required'],
[['organization', 'color'], 'integer'],
[['organization', 'color'], 'filter', 'filter' => 'intval'],

In the view, these attributes are formed by the dropDownList widget

In the controller, after
$model->load(Yii::$app->request->post())
if
var_dump($model->getDirtyAttributes());
they are displayed, they will be char and not int, although the values ​​​​have not changed, they are marked as "dirty" due to the fact that the old data was int and the new char

In view, if you do var_dump($ model ->color) ; will be INT

Where to dig?

UPD: if you finish the textinput attribute in the rule and enter not a number, then the validator will give an error, i.e. the rule works but it doesn't result in int type when parsing POST data? It turns out they need to be "manually" lead? Or is there "best practices" for my case?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry Kim, 2020-11-30
@Tiasar

loadonly loads data into the model, and for validation, you need to run the methodvalidate

R
Ruslan, 2020-11-30
@Tiasar

Thanks a lot! I'm a bit dumb :D Everything worked great with

$model->load(Yii::$app->request->post()) && $model->validate()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question