Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?