I'm new in Larvel 4.2 here! How do I do a custom error messages in Laravel 4.2? And where do I put these codes? I've been using the defaults and I kind of wanted to use my own.
Asked
Active
Viewed 695 times
-1
-
Is this for custom error messages when you're using validation on a form? – James Sep 25 '15 at 04:24
-
yes i'm trying to use this when validating a form – BourneShady Sep 25 '15 at 04:32
-
The answer below is perfect for you then, simply place the code inside the controller function that the form uses and it will implement the custom error messages you define. – James Sep 25 '15 at 04:42
-
thanks i just don't know where to put it – BourneShady Sep 25 '15 at 05:20
-
I have added an answer to show you how to use it. – James Sep 25 '15 at 05:55
2 Answers
1
Did you try something? http://laravel.com/docs/4.2/validation#custom-error-messages
Did you use Google? Check the documentation (official) it has everything. Be less lazy.
$messages = array(
'required' => 'The :attribute field is required.',
);
$validator = Validator::make($input, $rules, $messages);

Matt Komarnicki
- 5,198
- 7
- 40
- 92
-
im sorry if i sounded lazy and stupid. thanks i just don't know where to put it – BourneShady Sep 25 '15 at 05:21
-
1I didn't say you're stupid. Doing something on your own at first is the best teacher. Then ask... Enjoy Laravel - it's really good piece of stuff. – Matt Komarnicki Sep 25 '15 at 05:26
1
To add to the answer given by slick, here is how you could use it in a real example of a store function inside a controller:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'id1' => 'required|between:60,512',
'id2' => 'required|between:60,512',
'id3' => 'required|unique:table',
], [
'id1.required' => 'The first field is empty!',
'id2.required' => 'The second field is empty!',
'id3.required' => 'The third field is empty!',
'id1.between' => 'The first field must be between :min - :max characters long.',
'id2.between' => 'The second answer must be between :min - :max characters long.',
'id3.unique' => 'The third field must be unique in the table.',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
//... Do something like store the data entered in to the form
}
Where the id
should be the ID of the field in the form you want to validate.
You can check out all the rules you can use here.

James
- 15,754
- 12
- 73
- 91