show custom messages is not working properly - laravel

I have this validation rules where I want to show some custom messages instead of the custom messages. But like below is not working the validation messages appear like:
The participant.1.name field is required.
The participant.1.surname field is required.
But it should appear like:
The field name is required
The field surname is required
Validation rules:
$this->validate($request,
[
'participant.*.name' => 'required|string',
'participant.*.surname' => 'required|string',
],
[
'participant.*.name' => 'The field name is required.',
'participant.*.name' => 'Please insert a text value for the name field.',
'participant.*.surname' => 'The field surname is required',
'participant.*.surname' => 'Please insert a text value for the surname field'
]
);

You can customize your validation messages, For more details,
https://laravel.com/docs/5.6/validation#customizing-the-error-messages
Also, please try this,
$messages = [
'participant.1.name.required' => 'The Participant name is required.',
];
$validator = Validator::make($input, $rules, $messages);

Related

Show name of fields in message in Input Array Validation in Laravel

I have array of dropdown with the same name what i'm trying to do is
to display the actual name of the field when the error is displayed but now it is displaying like
attributes.Size Attributes Fields are required
where size is the title that i'm passing dynamically
but i want actual name which i am passing as a title to an array.
Code:
$validator = Validator::make($request->all(), [
'attributes.*' => 'required',
],[
'attributes.*.required' => ':attribute Attributes Fields are required.',
]);
{!! Form::select('attributes['.$attr->title.']') !!}

Laravel 5.8 | Test JSON response like assertJsonMessage?

I want to test the json test response contain different values, in this case error messages, like so:
public function testFieldsAreRequired()
{
$this->postJson('/posts', [])
->assertStatus(422)
->assertJsonValidationErrors([
'name',
'email',
'date_of_birth',
])
->assertJsonFragment([
'The name field is required.'
])
->assertJsonFragment([
'The email field is required.'
])
->assertJsonFragment([
'The date of birth field is required.'
]);
}
This approach is not very efficient. I'm looking for an approach, like so:
.....
->assertJsonMessage([
'The name field is required.',
'The email field is required.',
'The date of birth field is required',
]);
Like above, I don't need to specify keys and can specfiy different values in one array. So it's very efficient and manageable.
But assertJsonMessage is a protected method, so I'm not sure about original design and using this approach.

Change, error response displays a single error message in Laravel 5.4

I am using required_without validation for required any one field.
return Validator::make($data, [
'user_email' => 'required_without:user_phone|email',
'user_phone' => 'required_without:user_email|min:10',
'country_code' => 'required_with:user_phone',
'otp' => 'required|numeric|max:6',
]);
And How to user Unique for check email or phone registered or not in my tbl_users
And I have got this error message,I want to display one one line for user_email or user_phone.
And I am using country_code if user_phone.
I have got these error messages in response.
{
"user_email": [
"The user email field is required when user phone is not present."
],
"user_phone": [
"The user phone field is required when user email is not present."
]
}
i want this type of error message with s, Like :
{
"status":false,
"message":"Please enter email or phone"
}
You can use third param to add message manually, remove required_without for user_email and keep it for user_phone
$messages = [
'user_phone.required_without' => 'Please enter email or phone',
]
return Validator::make($data, [
'user_email' => 'user_phone|email',
'user_phone' => 'required_without:user_email|min:10',
'country_code' => 'required_with:user_phone',
'otp' => 'required|numeric|max:6',
],$messages);
Now you will get error message for only user_phone as Please enter email or phone

Do custom error messages in Laravel 4.2

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.

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