S
S
Sergey2016-07-29 02:54:43
Yii
Sergey, 2016-07-29 02:54:43

How to accept integer data in yii2?

Gentlemen, there is a form. The form submits lines, of course.
In one of the forms for deleting a user, a hidden input with id is submitted.
id must be an integer.
If I write in rules like this:

public function rules() {
        return [
        	['userid', 'required'],
        	['userid', 'integer', 'max' => 32, 'message' => 'Value should to be an integer'],
        ];
}

That validation does not work in principle, because userid is a string at POST
Are there any better validation methods than writing your own validation function, where the string is checked against a regular expression? Or in yii2 is it somehow possible to force the type to be converted before validation or to do something even more feng shui?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kim, 2016-07-29
@kimono

Actually, when checking, a string is parsed, so if you specify that the value should be an integer, then a pattern is checked: integerPattern .
So exactly what you want will come. Everything should be fine.

M
Mykola, 2016-07-29
@iSensetivity

[['userid'], 'integer', 'max' => 32, 'message' => 'Value should to be an integer']

A
Anton, 2016-07-29
Reytarovsky @Antonchik

['userid','number']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question