Yii2: Custom message for min and max validation rule

Created on 1 Jul 2015  路  6Comments  路  Source: yiisoft/yii2

RegisterForm.php:

    public function rules()
    {
        return [
            [['username', 'password', 'repeatPassword', 'email', 'firstName', 'lastName'], 'filter', 'filter' => 'trim'],
            [['username', 'password', 'repeatPassword', 'email', 'firstName', 'lastName'], 'required', 'message' => 'enter a value for {attribute}'],

            [['username', 'email', 'firstName', 'lastName'], 'string', 'min' => 2, 'max' => 256, 'message' => '{attribute} should be at least 2 symbols'],

            ['email', 'email', 'message' => 'enter valid e-mail'],

            ['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'this username has already been taken'],
            ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'this email address has already been taken'],

            ['password', 'string', 'min' => 6, 'max' => 256, 'message' => '{attribute} should be at least 6 symbols'],
        ];
    }

Result:

yii

As you can see, for 'min' => 6, 'max' displays default message. I expected my own message from my own rule.

Most helpful comment

String validator has got 3 messages:
'message' => error message used when the value is not a string.
'tooLong' => error message used when the length of the value is greater than $max.
'tooShort' => error message used when the length of the value is smaller than $min.
Replace the proper one.

All 6 comments

String validator has got 3 messages:
'message' => error message used when the value is not a string.
'tooLong' => error message used when the length of the value is greater than $max.
'tooShort' => error message used when the length of the value is smaller than $min.
Replace the proper one.

@bizley-code thanks a lot.

I have same problem
I have defined following rules for phone
[['phone','ci_mobileno', 'trainer_mobile'], 'string','min'=>10, 'max' => 10,'message'=>'Should be 10 digit long.(Example: 9012687986)'],
but it is showing the below message
Phone should contain at least 10 characters.

@sktandon0121 use length to set a fixed length.

stringvalidator tooShort - tooLong

Click here.

Example 1
[['username', 'email', 'firstName', 'lastName'], 'string', 'min' => 2, 'max' => 256, 'tooShort' => '{attribute} should be at least 2 symbols' , 'tooLong' => '{attribute} should be at most 256 symbols' ],


Example 2
[['phone','ci_mobileno', 'trainer_mobile'], 'string','min'=>10, 'max' => 10,'tooShort'=>'Should be 10 digit long.(Example: 9012687986)' , 'tooLong' => 'Should be 10 digit long.(Example: 9012687986)' ],

numbervalidator tooBig - tooSmall

Click here.

public string $tooBig = null
User-defined error message used when the value is bigger than $max.

public string $tooSmall = null
User-defined error message used when the value is smaller than $min.

tooShort/tooLong not work for integer validator.
Setting unknown property: yii\validators\NumberValidator::tooShort

use tooSmall/tooBig for similar behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexandernst picture alexandernst  路  163Comments

Faryshta picture Faryshta  路  48Comments

sapsxxxil picture sapsxxxil  路  50Comments

Mirocow picture Mirocow  路  56Comments

spiritdead picture spiritdead  路  67Comments