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.
Related
How can I validate my inputs from a GET method?
Example URL: localhost:8000?salary=2000&name=sample&description=vowewljfodigjfdglfd
In the URL I have 3 inputs and I want to validate:
Salary - should accept only numeric
Name - should accept only alphabetic
Description - should accept with max:1000
Somebody knows how to do this?
The Laravel validator doesn't care where the data came from. You can manually create a validator and pass it the query string data.
$validator = Validator::make($request->query(), [
'salary' => 'numeric',
'name' => 'alpha_num',
'description' => 'max:1000',
]);
if ($validator->fails()) {
// show an error
}
Side note: As someone with a hyphenated last name, I implore you not to treat name as alphanumeric. See Falsehoods Programmers Believe About Names.
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);
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.
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.
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.