验证器中提到使用$message参数来定义验证的错误消息,此节详细介绍错误消息的定义

message变量

验证器以及规则对象中的错误消息$message参数支持使用:

  • :attribute 当前验证的字段名称
  • :input 当前验证的值
  • :index 当前验证的数组索引
  • {:field} 指定字段的值
  • {@field} 指定字段的名称
  • {&index} 获取触发错误中的数组索引 4.2.1
  • {&&index} 获取触发错误中的数组位置 4.2.1

说明

上述文本在自定义规则中均支持使用

:attribute

:attribute 代表为当前触发错误的customAttributes变量中的字段名称

class Test extends Validate
{
    protected $rule = [
        'user' => 'required'
    ];
    
    protected $message = [
        'user.required' => '请填写:attribute'
    ];
    
    protected $customAttributes = [
        'user' => '账号'
    ];
}







 






触发后,提示的消息为请填写账号

{:field}

{:field}中间的field为当前验证值的字段,如果指定字段不存在,则为空文本,支持获取数组,如info.name,代表获取info数组中的name参数,可无限下层

class Test extends Validate
{
    protected $rule = [
        'name' => 'chs'
    ];
    
    protected $message = [
        'name.chs' => '你填写的名字{:name}不是中国名字'
    ];
}







 


输入数据['name' => 'Rasmus Lerdorf'],提示:你填写的名字Rasmus Lerdorf不是中国名字

数组

如果触发错误的是数组中的某个值,也可以直接获取:{:data.*.user.name}

{@field}

{@field}表示使用field的自定义名称

class User extends Validate
{
    protected $rule = [
        'pass'   => 'required',
        'repass' => 'required|same:pass'
    ];

    protected $customAttributes = [
        'pass'   => '密码',
        'repass' => '确认密码'
    ];

    protected $message = [
        'repass.same' => ':attribute和{@pass}不一致'
    ];
}













 


最后触发的错误消息为:确认密码和密码不一致

{&index}和{&&index}

如果在验证中有一个多维数组触发了错误,我们需要在错误消息中用到索引,或者需要数组中的某个字段值。

index为数组中的第N个索引

class Test extends Validate
{
    protected $rule = [
        'button'                     => 'required|array:@keyInt|max:3',
        'button.*'                   => 'required|array:name,sub_button',
        'button.*.name'              => 'required|chs_dash',
        'button.*.sub_button'        => 'required|array:@keyInt',
        'button.*.sub_button.*'      => 'required|array:name,type',
        'button.*.sub_button.*.name' => 'required|chs_dash',
        'button.*.sub_button.*.type' => 'required|numeric',
    ];

    protected $message = [
        'button.*.sub_button.*.type.required' => '{:button.*.name} 的第 {&&1} 子菜单的类型缺失,索引为{&1}'
    ];
}
Test::make()->check([
    'button' => [
        [
            'name'       => '主菜单1',
            'sub_button' => [
                [
                    'name' => '子菜单1-1',
                    'type' => 1
                ],
                [
                    'name' => '子菜单1-2',
                    'type' => null
                ]
            ]
        ]
    ]
]);

会输出错误主菜单1 的第 2 子菜单的类型缺失,索引为1

联合使用

也可以{:field}一起来取到需要的值,如:{:button.{&0}.sub_button.{&1}.name}

customAttributes变量

当我们定义了大量的验证字段和规则时,如果一个一个对应的编写错误消息,需要耗费大量的时间成本,这个时候,我们可以使用$customAttributes变量定义字段的名称。

当错误触发时,会自动替换默认错误消息中的:attribute文本

class User extends Validate
{
    protected $rule = [
        'id' => 'required|numeric',
    ];

    protected $customAttributes = [
        'id' => '字段ID',
    ];
}






 
 
 

当错误触发时,会提示字段ID 不可为空,字段ID 必须为数字 customAttributes变量中也支持{:field},如:

protected $customAttributes = [
    'id' => '字段ID:{:id}',
];

 

如果传入idhello

此时触发后会提示字段ID:hello 必须为数字

规则类中格式化错误消息

上诉代码中,错误提示可能并不是那么清晰,将$size变量放入错误提示,也许会更好,我们提供了$messageParam参数,用于支持格式化错误消息,如下:

class Length extends BaseRule
{
    protected $message = ':attribute的长度需为%d个字节';

    protected $size;

    public function __construct(int $size)
    {
        $this->size         = $size;
        $this->messageParam = [$size];
    }

    public function passes($attribute, $value): bool
    {
        return strlen($value) === $this->size;
    }
}










 








$messageParam是一个数组类型,所以传入的值必须为数组,如使用规则为length:10,则触发后的消息为::attribute的长度需为10个字节

$message字段定义:

格式化格式说明
%%返回一个百分号 %
%b二进制数
%cASCII 值对应的字符
%d包含正负号的十进制数(负数、0、正数)
%e使用小写的科学计数法(例如 1.2e+2)
%E使用大写的科学计数法(例如 1.2E+2)
%u不包含正负号的十进制数(大于等于 0)
%f浮点数(本地设置)
%F浮点数(非本地设置)
%g较短的 %e 和 %f
%G较短的 %E 和 %f
%o八进制数
%s字符串
%x十六进制数(小写字母)
%X十六进制数(大写字母)

附加的格式值。必需放置在 % 和字母之间(例如 %.2f):

  • \+ (在数字前面加上 + 或 - 来定义数字的正负性。默认情况下,只有负数才做标记,正数不做标记)
  • ' (规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。例如:%'x20s(使用 "x" 作为填充))
  • \- (左调整变量值)
  • [0-9] (规定变量值的最小宽度)
  • .[0-9] (规定小数位数或最大字符串长度)

注意

如果使用多个上述的格式值,$messageParam的参数必须按照上面的顺序进行使用,不能打乱。

关于错误消息的更多支持请查看自定义错误消息

规则类中使用类属性

有时候,我们需要使用一个更为直观的错误消息定义方法,并且无需格式转换,可以直接在错误消息中使用规则类中的属性,格式为:%{字段名}

注意

仅支持public,protected类型,且值必须为标量在新窗口打开

可以将上诉代码中的规则改为如下所示:

class Length extends BaseRule
{
    protected $message = ':attribute的长度需为%{size}个字节';

    protected $size;

    public function __construct(int $size)
    {
        $this->size = $size;
    }

    public function passes($attribute, $value): bool
    {
        return strlen($value) === $this->size;
    }
}



 














如果要使用数组下的某个值,可以使用和验证器集合的get一致的取值方法。

数组形式的错误消息 4.3.0

错误消息也支持以下形式的定义

class TestArrayMessageValidate extends Validate
{
    protected $rule = [
        'user' => 'required|min:3|max:8',
        'pass' => 'required|min:3|max:8',
    ];

    protected $message = [
        'user' => [
            'required' => '用户名必须填写',
            'min'      => '用户名的长度不可低于 :min',
            'max'      => '用户名的长度不可超过 :max',
        ],
        'pass' => [
            'required' => '密码必须填写',
            'min'      => '密码的长度不可低于 :min',
            'max'      => '密码的长度不可超过 :max',
        ]
    ];
}







 
 
 
 
 
 
 
 
 
 
 


效果等同于

class TestArrayMessageValidate extends Validate
{
    protected $rule = [
        'user' => 'required|min:3|max:8',
        'pass' => 'required|min:3|max:8',
    ];

    protected $message = [
        'user.required' => '用户名必须填写',
        'user.min'      => '用户名的长度不可低于 :min',
        'user.max'      => '用户名的长度不可超过 :max',
        'pass.required' => '密码必须填写',
        'pass.min'      => '密码的长度不可低于 :min',
        'pass.max'      => '密码的长度不可超过 :max',
    ];
}