Laravel验证自定义消息


Laravel Validation custom message

我面临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.',
];

现在,如果地址或电话号码有问题,因为两者都有regex验证规则,错误消息将是::attribute number必须格式化为:xxx xxx xxxx,我如何为每个不同的号码都有自定义消息??

以下是实现这一点的方法,而不是使用"regex",而是使用"phone.regex"

$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.',
    'phone.regex'   => 'The :attribute number is invalid , accepted format: xxx-xxx-xxxx',
    'address.regex' => 'The :attribute format is invalid.',
];