Yii2中Model中rules中自定义message无效的问题原因

  •   
  • 8958
  • PHP
  • 35
  • super_dodo
  • 2016/09/21

今天在使用Yii2中的Model里面的rules规则的时候,希望验证规则不通过的时候出现自己自定义的提示信息。但是按照常规写法部分能按照自定义的语句提示(require integer...),但是对于有max min的就没有按照自定义的形式输出了。

经过查询相关资料以及Yii2的源码yii.validation.js里面有了新的定义tooLong tooShort tooBig tooSmall 等,和Yii1有一定的区别。

如果需要查看相关的Yii2的rule规则请猛击:Yii2中Model的一些常用rules验证规则

public function rules(){
	return [
		[['name','age'], 'required','message'=>'{attribute}不能为空!!!'],
		[['age'], 'integer','message'=>'{attribute}为数值类型!!!'],		//如果只验证类型的话
		[['age'], 'integer','min'=>10,'max'=>100,'tooSmall'=>'{attribute}不能小于10岁','tooBig'=>'{attribute}不能大于100岁'],
		[['name'], 'string', 'min'=>3,'max'=>10,'tooShort'=>'{attribute}长度不能小于3字符','tooLong'=>'{attribute}长度不能大于10个字符'],
		[['address'], 'string','max'=>10,'tooLong'=>'{attribute}字符长度过长!'],
		[['remark'], 'safe'],
	];
}

public function attributeLabels(){
	return [
		'id' => 'ID',
		'name' => '姓名',
		'age' => '年龄',
		'address' => '地址',
		'remark' => '备注说明',	
	];
}

// 获取 Model 错误信息中的 第一条, 无错误时 返回 null
public static function getModelError($model) {
	$errors = $model->getErrors();
	if(!is_array($errors)) return '';
	$firstError = array_shift($errors);
	if(!is_array($firstError)) return '';
	return array_shift($firstError);
}

其实Yii2的错误提示信息已经很完整了,可以直接不需要重写message直接使用默认的即可。语言设置成为中文zh-CN设置方法请移步修改Yii2的默认语言language为中文zh-CN的方法

附上yii2的默认的错误提示语中文 yiisoft\yii2\message\zh-CN\yii.php

return [
	'{attribute} cannot be blank.' => '{attribute}不能为空。',
	'{attribute} is invalid.' => '{attribute}是无效的。',
	'{attribute} is not a valid URL.' => '{attribute}不是一条有效的URL。',
	'{attribute} is not a valid email address.' => '{attribute}不是有效的邮箱地址。',
	'{attribute} must be "{requiredValue}".' => '{attribute}必须为"{requiredValue}"。',
	'{attribute} must be a number.' => '{attribute}必须是一个数字。',
	'{attribute} must be a string.' => '{attribute}必须是一条字符串。',
	'{attribute} must be an integer.' => '{attribute}必须是整数。',
	'{attribute} must be either "{true}" or "{false}".' => '{attribute}的值必须要么为"{true}",要么为"{false}"。',
	'{attribute} must be equal to "{compareValueOrAttribute}".' => '{attribute}的值必须等于"{compareValueOrAttribute}"。',
	'{attribute} must not be equal to "{compareValueOrAttribute}".' => '{attribute}的值不得等于"{compareValueOrAttribute}"。',
	'{attribute} must be greater than "{compareValueOrAttribute}".' => '{attribute}的值必须大于"{compareValueOrAttribute}"。',
	'{attribute} must be greater than or equal to "{compareValueOrAttribute}".' => '{attribute}的值必须大于或等于"{compareValueOrAttribute}"。',
	'{attribute} must be less than "{compareValueOrAttribute}".' => '{attribute}的值必须小于"{compareValueOrAttribute}"。',
	'{attribute} must be less than or equal to "{compareValueOrAttribute}".' => '{attribute}的值必须小于或等于"{compareValueOrAttribute}"。',
	'{attribute} must be no greater than {max}.' => '{attribute}的值必须不大于{max}。',
	'{attribute} must be no less than {min}.' => '{attribute}的值必须不小于{min}。',
	'{attribute} must be repeated exactly.' => '{attribute}必须重复。',
	'{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.' => '{attribute}应该包含至少{min, number}个字符。',
	'{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.' => '{attribute}只能包含至多{max, number}个字符。',
	'{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.' => '{attribute}应该包含{length, number}个字符。',
]

Yii2中Model的一些常用rules验证规则

修改Yii2的默认语言language为中文zh-CN的方法