Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Validation & Flash Messages Sessions and Messages

Is there a way of modify the content of the error messages?

I would like to translate the content to other languages or change it to more friendly messages.

4 Answers

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Yes, there is a few ways. Are you using Laravel 4 or 5? I'm still using L4, so in case you're using the same, just go into the app/lang/??/validation.php. Near the bottom is a nested array that you can fill as needed.

Another way is to just pass an array of your custom messages right into Validator.

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

No problem! You just pass them into the Validator::make() method like so, with the :attribute placeholder:

$messages = [
    'max:500' => 'Sorry, the :attribute is too big.',
    'required' => 'We need a :attribute from you to continue.'
];

Validator::make($data, $rules, $messages);

EDIT: I just checked the docs: http://laravel.com/docs/4.2/validation

It seems you can also get even more specific like their example here:

$messages = array(
    'email.required' => 'We need to know your e-mail address!',
);

I think actually this Validator method is the cleanest and easiest to read since you can keep it all together in the model. Wasn't using it before but will from now on.

Cool! Thank you very much Łukasz, it will help a lot! :)

I'm using the 4. Yes, I found this file. Thank you very much! :)

How would work this option where I send a array of custom messages to the Validator?