14

I am facing a problem with laravel custom validation message, here is what I have:

$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.',
];

Now if there is a problem with the address or the phone number since both have regex validation rule , the error message will be : The :attribute number has to be formated : xxx-xxx-xxxx, How could I have custom message for each different one ??

5
  • possible duplicate of How to specify the default error message when extending the Validation class in Laravel 4 Commented Apr 16, 2014 at 19:04
  • actually I just solved it now , :) all you need is this Commented Apr 16, 2014 at 19:07
  • If you solved this, please answer your question to benefit future SO users. Commented Apr 16, 2014 at 19:07
  • $messages = array( '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.',); Commented Apr 16, 2014 at 19:08
  • 'fieldname.unique' or 'fieldname.regex' Commented Apr 16, 2014 at 19:08

1 Answer 1

20

Here is the way to do it , just instead of using 'regex' , use '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.',
];
Sign up to request clarification or add additional context in comments.

1 Comment

didn't know it until today :) i have also removed an extra array closing-a syntax error :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.