【发布时间】:2014-05-31 19:56:43
【问题描述】:
我遇到了 laravel 自定义验证消息的问题,这是我所拥有的:
$rules = [
'first_name' => 'required|alpha|min:2',
'last_name' => 'required|alpha|min:2',
'email' => 'required|email|unique:users,email,' . Input::get('id') . ',id',
'password' => 'alpha_num|between:6,12|confirmed',
'password_confirmation' => 'alpha_num|between:6,12',
'address' => 'regex:/^[a-z0-9- ]+$/i|min:2',
'city' => 'alpha|min:2',
'state' => 'alpha|min:2|max:2',
'zip' => 'numeric|min:5|max:5',
'phone' => 'regex:/^\d{3}\-\d{3}\-\d{4}$/',
];
$messages = [
'unique' => 'The :attribute already been registered.',
'regex' => 'The :attribute number has to be formated : xxx-xxx-xxxx.',
];
现在如果地址或电话号码有问题,因为两者都有正则表达式验证规则,错误消息将是::属性号码必须格式化:xxx-xxx-xxxx,我怎么能有自定义消息每个不同的??
【问题讨论】:
-
其实我现在刚刚解决了,:) 你只需要这个
-
如果您解决了这个问题,请回答您的问题以造福未来的 SO 用户。
-
$messages = array( 'unique' => ':attribute 已注册。','phone.regex' => ':attribute 编号无效,接受格式:xxx-xxx- xxxx','address.regex' => ':attribute 格式无效',);
-
'fieldname.unique' 或 'fieldname.regex'