carloscc
carloscc

Reputation: 799

Custom validation messages to specific field in laravel

I am wondering if I can create a custom validation message for a specific field. For example i have a field named firstname, laravel so the validation error message with the if firstname (not capital letters and all together) so it looks bad. I want to create a specific message to just this field and it has to be on the validation.php because my site has different languages. Is it possible?

And if I have a field called email? I know email is already a validation message, so can I create a custom validation message to just the fields called email. Without matter if they have the email validation condition.

Upvotes: 4

Views: 3802

Answers (3)

Matheus Evangelista
Matheus Evangelista

Reputation: 31

Yes, you can use this custom message like this way:

    $messages = [
        'required' => 'The :attribute field is required.',
        'birthdate.required' => 'The BIRTHDATE field is required.'
    ];

    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'birthdate' => 'required',
    ], $messages);

Upvotes: 3

Bhaskar Thammali
Bhaskar Thammali

Reputation: 56

Yes, You can.

Use this custom message to add your custom message specific to a field

//field_rule => 'your message'
$messages = array(
'email_required' => 'The :attribute field is required.'
);

Upvotes: 4

Faruk Omar
Faruk Omar

Reputation: 1193

You can change your validation message by custom error message setting , details

Now based on you language you just change $messages array

$messages = array(
    'required' => 'The :attribute field is required.',
    'same'    => 'The :attribute and :other must match.',
    'size'    => 'The :attribute must be exactly :size.',
    'between' => 'The :attribute must be between :min - :max.',
    'in'      => 'The :attribute must be one of the following types: :values',
);

$validator = Validator::make($input, $rules, $messages);

Upvotes: 0

Related Questions