Do custom error messages in Laravel 4.2 - laravel

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.

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);

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.

Related

How to get the error type of Validator on Laravel? [duplicate]

If there a way to check whether or not the validator failed specifically because of the unique rule?
$rules = array(
'email_address' => 'required|email|unique:users,email',
'postal_code' => 'required|alpha_num',
);
$messages = array(
'required' => 'The :attribute field is required',
'email' => 'The :attribute field is required',
'alpha_num' => 'The :attribute field must only be letters and numbers (no spaces)'
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails()) {
In laymans terms, I basically want to know: "did the validation fail because the email_address was not unique?"
Check for a specific rule within the returned array of failed rules
if ($validator->fails()) {
$failedRules = $validator->failed();
if(isset($failedRules['email_address']['Unique'])) {
...
This will display an error and tell you what failed:
Controller
if($validation->fails()){
return Redirect::back()->withErrors($validation)->withInput();
}
foreach($errors->all() as $error) {
echo $error;
}
And in your blade template add this:
#foreach($errors->all() as $error)
<div>
{{$error}}
</div>
#endforeach
And that will return a message with whatever the error is. Email doesn't match. Field is required. Blah blah
You can also remove that email array from the $message. The validator will handle all that for you. You only want to use that if you want custom messages.
You can also try to var_dump this statement:
var_dump($validation->errors()); die;

How to make 2 different Laravel Validation messages for the same rule that has 2 different arguments?

Hello I have Laravel validation request with these rules
public function rules()
{
return [
'email' => 'unique:users,email|required|unique:invitations,email',
'name' => 'string|required',
];
}
I want that "unique:users,email" would display one message and "unique:invitations,email" display other message. How to do that?
'unique:users,email' => 'This E-Mail address is already registered.',
'unique:invitations,email' => 'Invitation to this E-Mail address is already sent.',
It always returns default message for "unique" rule.
'unique' => 'The :attribute has already been taken.',
I am not sure if this will work but can you try these
public function messages()
{
return [
'unique:users,email' => 'This E-Mail address is already registered.',
'unique:invitations,email' => 'Invitation to this E-Mail address is already sent.',
//or
'email.unique:users,emai' => 'This E-Mail address is already registered.',
'email.unique:invitations,email' => 'Invitation to this E-Mail address is
//you may even try it without the ,fieldname
'email.unique:users' => ''
];
}
Try to define like
'email.unique:users,emai' => ....,
'email.unique:invitations,emai' => ....,

How to custom validation with input value as name already been taken?

In controller
public function store(Request $req)
{
$req->validate([
'username' => 'required|unique:users',
]);
$users = new Users();
$users->username = $req->username;
$users->save();
return redirect()->route('list.users');
}
The username validation return as The username has already been taken.
I am typing the username field as JHON and i would to return the validation as like: The JHON has already been taken. ?
I believe you can use :input when defining custom validation messages.
public function store(Request $req)
{
Validator::make($req, [
'username' => 'required|unique:users',
], [
'unique' => 'The :input username has already been taken.',
]);
$users = new Users();
$users->username = $req->username;
$users->save();
return redirect()->route('list.users');
}
To make your code cleaner, you should change the localization of the "unique" validation error
Go to the file "resources/lang/en/validation.php" (or change the "en" part with your language's iso code) and change the "unique" value.
From:
'unique' => 'The :attribute has already been taken.',
To:
'unique' => 'The :attribute :input has already been taken.',
This way, you should get the following message
The username JHON has already been taken.
$req->validate([
'username' => 'required|unique:users,username',
]);
This is the syntax for using unique:
unique:table,column
https://laravel.com/docs/7.x/validation#rule-unique

Laravel Array Validation Message Value

For array validation messages, is there a way to display the value as opposed to the attribute? Doing so without using a custom validator.
Example:
$messages = [
‘*' => ':value is invalid.’
]
This would output something like "email#address is invalid".
Thanks for your help!
In case anyone is still looking with the latest of Laravel versions, the answer is to use the :input parameter in your message output:
'between' => 'The :attribute value :input is not between :min - :max.'
Docs: https://laravel.com/docs/5.7/validation#custom-error-messages
To access the index for the array validation I simply iterate over the elements I'm trying to validate instead of using the * wildcard.
public function messages()
{
$messages = [];
foreach($this->emails as $key => $email) {
$messages[$key] = $email . ' is an invalid email address.';
}
return $messages;
}
Hope this helps anyone who is having the same problem.
For fully custom strings you can pass custom messages as the third argument to the Validator::make() method. If you need only generic descriptors you can use some built place-holders such as :attribute, :size, or :values
For example:
$messages = ['required' => 'The :attribute field is required.'];
$validator = Validator::make($input, $rules, $messages);
:attribute will be replaced by the actual name of the field under validation.
More info can be found here.

Validation in laravel 4

Iam new in laravel.
Iam trying to laravel form validation.
my validation code is
$rules = array(
'studname'=>'required',
'pmobile'=>'required|digits:10',
'studadno'=>'required|unique:wys_students,studadno',
'studrollno'=>'required|numeric|unique:wys_students,studrollno',
'studgender'=>'required|in:male,female',
'studdob' =>'required',
'studbldgrp'=>'required|in:O+ve,O-ve,A+ve,A-ve,B+ve,B-ve,AB+ve,AB-ve,Other',
); $messages = array(
'required' => 'The :attribute field is required.',
'in' => 'The :attribute must be one of the following types: :values',
);
$validate=Validator::make(Input::all(), $rules, $messages); `
output is-:
Student Name :The studname field is required.
Parent Mobile:
The pmobile field is required.
but, I want eg: student name field is required..
how to change my validation code?
$messages = [
'studname' => 'student name field is required.',
];
$validator = Validator::make($input, $rules, $messages);
You can pass the $messages array as the third parameter and you can define custom messages in the $messages array
There are 2 options.
Firstly you can change the form field name to student_name and then you will see message "The student name field is required"
Secondly you can write custom messages for each of the fields. In you case it will be:
$messages = array(
'studname.required' => 'The Student Name field is required.'
);
$rules = array(
'studname' => 'required',
'pmobile'=>'required|digits:10',
'studadno'=>'required|unique:wys_students,studadno',
'studrollno'=>'required|numeric|unique:wys_students,studrollno',
'studgender'=>'required|in:male,female',
'studdob' =>'required',
'studbldgrp'=>'required|in:O+ve,O-ve,A+ve,A-ve,B+ve,B-ve,AB+ve,AB-ve,Other',
);
$messages = array(
'studname.required' =>'The Student Name field is required.',
'pmobile.required'=>'The Parent Moblie field is required.',
'studadno.required'=>'The Student Admission Nubmer field is required.',
'studrollno.required'=>'The Student RollNubmer field is required.',
'studgender.required'=>'Must be one of the following types: :values',
'studdob.required'=>'The Student Date of Birth field is required.',
'studbldgrp.in'=>'Must be one of the following types: :values',
);
$validate=Validator::make(Input::all(), $rules, $messages);
You can provide translations of the attribute names in app/lang/xx/validation.php.
There's an attributes array at the very bottom, just add your attributes:
'attributes' => array(
'studname' => 'student name',
'pmobile' => 'parent mobile',
// and so on
),
This way you don't have to rewrite the message for every field and every rule. And you can even use those field names across multiple forms.

Resources