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:
As you can see, for 'min' => 6, 'max' displays default message. I expected my own message from my own rule.
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.
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)' ],
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.
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.