Vinit Singh Vinit Singh - 4 months ago 49
PHP Question

Custom validation message not working in Yii2

I want to display custom validation messages for max length and min length of phone number.
I have written this code to display custom validation message for max length and min length of phone number field.

['phone', 'string', 'max' => 12, 'min' => 8, 'message' => 'Please enter a valid Phone number.']


The validation is working well however custom message is not being displayed. everytime is shows this default yii2 message "Phone should contain at most 12 characters."

Thanks in advance for help.

Answer

Try:

Update:

Change the type from string to number if you want to use max and min property.

['phone', 'number', 'max' => 12, 'min' => 8, 'tooBig' => 'Please enter a valid Phone number.', 'tooSmall' => 'Please enter a valid Phone number.']