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.']') !!}
Related
I have a problem when I validate array with required_with validation. It is always showing even I input nothing . I got this error since two days ago. I want to check if one field insert value ,the other filed must insert value .If nothing insert , the other filed don't need to insert. Could you help me please?
This is Error I got even I input value or not . Is there any solutions for this ?
Error Message Image
Validation Request Code.
'work_procedure[]'=>'nullable|required_with:times,work_points',
'times[]'=>'nullable|required_with:work_procedure',
'work_points[]'=>'nullable|required_with:work_procedure',
This is Model.php
protected $fillable=[
'work_procedure',
'times',
'work_points',
];
https://laravel.com/docs/8.x/validation#rule-required-with-all
required_with_all:foo,bar,...
The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
example
$request->validate([
'name' => 'required',
'surname' => 'required_with:name'
]);
$request->validate([
'name' => 'required',
'detail' => 'required|required_with:name',
]);
Validator::make($request->all(), [
'role_id' => Rule::required_with_all:foo,bar,
]);
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);
I would like to ask for some advices about Laravel Validation...
Let's say I've got an input named invoiceAddress[name] and in a controller I've got a rule
$rule = ['invoiceAddress.name' => 'required',];
or just a
$validator = \Validator::make($request->all(), [
'invoiceAddress.name' => 'required',
]);
now, inside custom validation language file validation.php, am I able to nest attributes somehow? like:
'required' => ':attribute is mandatory',
'attributes' => [
'invoiceAddress' => [
'name' => 'blahblah'
],
],
If I try to nest the attribute the way above, i get
ErrorException
mb_strtoupper() expects parameter 1 to be string, array given
because I am using a field (as above)
['name' => 'blahblah']
I am trying to get custom messages using the file and the :attribute directive (as mentioned in the code above).
I am basically trying to do this How to set custom attribute labels for nested inputs in Laravel but i get the mentioned error...
Thank you in advance...
A Note On Nested Attributes
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
Reference: https://laravel.com/docs/5.3/validation#quick-writing-the-validation-logic (A Note On Nested Attributes Section)
I am simply needing to know how to validate my array of checkbox values to be in a certain list of values. So, I am using the "in:" validation rule as shown below, but it's returning the error that the value of the checked box is invalid. Do I need to do something different since it's an array of values sent via AJAX?
Controller:
if ($request->ajax())
{
/*Create record in UserType Model with the values from the Type checkboxes*/
$Type = Input::get('Type');
$this->validate($request, [
'Type' => 'required|in:A,B,C,D',
],[
'required' => 'You must select at least one.',
]);
foreach ($Type as $key => $value)
{
Auth::user()->userType()->create([
'profile_id' => Auth::user()->id,
'type' => $value,
]);
}
}
In my form, I have the following inputs...
<input type="checkbox" name="Type[]" value="A"/>
<input type="checkbox" name="Type[]" value="B"/>
<input type="checkbox" name="Type[]" value="C"/>
UPDATE:
So, I found in the Laravel documentation that you can validate arrays using * to get the key/values in an array. However, in my array it's just Type[] so I've tried the following with no luck.
$this->validate($request,[
'type.*' => 'required|in:A,B,C,D'
// or
'type*' => 'required|in:A,B,C,D'
]);
Just not working. I know I need to retrieve the array value something with the *.
UPDATE:
I was running Laravel 5.1 when this option for validation is only available in Laravel 5.2. Solved.
First: The required isn't necessary when using in because it must be A, B or C. That is like a "multiple" required already.
Second: As shown in the docs just use:
$this->validate($request,[
'Type.*' => 'in:A,B,C,D'
],[
'in' => 'You must select at least one.',
]);
As your Input array is named Type. That would validate all Inputs named Type.
To be clear the asteriks could be replaced by e.g. one to just validate the Input named Type[one]:
'Type.one' => 'in:A,B,C,D'
or if the Input would be named Type[one][name]
'Type.one.name' => 'in:A,B,C,D'
You try this
if($this->has('Type') && is_array($this->get('Type'))){
foreach($this->get('Type') as $key => $Type){
$rules['Type'.$key.'] = 'required|in:A,B,C,D';
}
In laravel 5 ..
$this->validate($request, [
'Type' => 'required|array'
]);
Worked for me for checkboxes
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.