In an app based on auto-generated Yii 2 Advanced Project Template I have modified 37th line of /frontend/models/SignupForm.php model (the line that uses yii\validators\StringValidator to validate length of the password). Changing it from:
['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength']],
Into:
['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength'], 'message' => 'Test'],
This seems to be ignored by the app and I am still getting default, framework-based message:
Password should contain at least 8 characters.
I have tried to change order of the parameters (which is totally pointless probably) into:
['password', 'string', 'message' => 'Test', 'min' => Yii::$app->params['user.passwordMinLength']],
Again, no effect.
In the very same place (rules()
method of SignupForm
model) I have many more validation rules (all of them comes from auto-generated app, no change on my side). I have modified messages displayed to the end user in all of them. All modifications are working and I see my custom message for every field and every validator (required
, unique
, email
, etc.).
The only problem is with the string
validator (yii\validators\StringValidator). It does display framework-default and not custom messages for both fields used in the signup form.
Here is the entire code of this method:
public function rules()
{
return [
['username', 'trim'],
['username', 'required', 'message' => Yii::t('frontend-models', 'This field cannot be blank.')],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('frontend-models', 'This username has already been taken.')],
['username', 'string', 'min' => 2, 'max' => 255, 'message' => 'Test'],
['email', 'trim'],
['email', 'required', 'message' => Yii::t('frontend-models', 'This field cannot be blank.')],
['email', 'email', 'message' => Yii::t('frontend-models', 'This field field must contain a valid e-mail address.')],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('frontend-models', 'This email address has already been taken.')],
['password', 'required', 'message' => Yii::t('frontend-models', 'This field cannot be blank.')],
['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength'], 'message' => 'Test'],
];
}
What am I missing?