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 ??

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
user3150060
  • 1,725
  • 7
  • 26
  • 46
  • possible duplicate of [How to specify the default error message when extending the Validation class in Laravel 4](http://stackoverflow.com/questions/17647044/how-to-specify-the-default-error-message-when-extending-the-validation-class-in) – Tomasz Kowalczyk Apr 16 '14 at 19:04
  • actually I just solved it now , :) all you need is this – user3150060 Apr 16 '14 at 19:07
  • If you solved this, please answer your question to benefit future SO users. – Tomasz Kowalczyk Apr 16 '14 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.',); – user3150060 Apr 16 '14 at 19:08
  • 'fieldname.unique' or 'fieldname.regex' – user3150060 Apr 16 '14 at 19:08

1 Answers1

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.',
];
C Taque
  • 997
  • 2
  • 15
  • 31
user3150060
  • 1,725
  • 7
  • 26
  • 46