laravel validation to check multi dimensional array - laravel

I have two fields
data[0][student]
data[0][teacher]
data[1][student]
data[1][teacher]
I tried this rule
$rules['data.*.student'] = 'required';
which gives this error
{
"error": {
"status_code": 412,
"validation": {
"data.student.*": [
"The data.*.student field is required."
]
},
"message": "Validation Failed"
}
}
how can I achieve this and make fields required if user missed this input field
data[student][0]?

For that you need to set a custom messages for rules inside Formrequest file.
You need to add rules and messages function Like this demostrated example:
Rules
public function rules()
{
return [
'item.*.name' => 'required|string|max:255',
'item.*.description' => 'sometimes|nullable|string|min:60',
'sku' => 'required|array',
'sku.*' => 'sometimes|required|string|regex:​​/^[a-zA-Z0-9]+$/',
'sku' => 'required|array',
'price.*' => 'sometimes|required|numeric'
];
}
Message
public function messages()
{
return [
'item.*.name.required' => 'You must have an item name.',
'item.*.name.max' => 'The item name must not surpass 255 characters.',
'item.*.description.min' => 'The description must have a minimum of 60 characters.',
'sku.*.regex' => 'The SKU must only have alphanumeric characters.',
'price.*.numeric' => 'You have invalid characters in the price field.'
];
}

Related

how to use js validator for two different forms in laravel?

how to use js validator rules for two different forms in laravel ?
given below the code in controller
private $rules = [
"remark" => "required",
"tender_doc_path" => "required",
];
private $messages = [
"remark.required" => "The Remark Field is required.",
"tender_doc_path.required" => "The Tender Doc Path Field is required.",
];
this rules are used for two different form how it should be conditioned , the tender doc
filed is not required for second form
public function appoint_agency_edit($id)
{
$validator = \JSValidator::make($this->rules,$this->messages);
return view('ree.architect.appoint_agency',compact('validator'));
}
You can create two instances of your validator. Make sure you pass the rules for each instance properly.
private $rules = [
'sampleOne' => [
"remark" => "required",
"tender_doc_path" => "required",
],
'sampleTwo' => [
"remark" => "required",
"tender_doc_path" => "required",
]
];
private $messages = [
'sampleOne' => [
"remark.required" => "The Remark Field is required.",
"tender_doc_path.required" => "The Tender Doc Path Field is required.",
],
'sampleTwo' => [
"remark.required" => "The Remark Field is required.",
"tender_doc_path.required" => "The Tender Doc Path Field is required.",
]
];
public function appoint_agency_edit($id)
{
return view('ree.architect.appoint_agency', [
'sampleOne' => \JSValidator::make($this->rules['sampleOne'], $this->messages['sampleOne'),
'sampleTwo' => \JSValidator::make($this->rules['sampleTwo'], $this->messages['sampleTwo'])
]);
}

Why the validation message are not appearing with required_if?

I want that the fields payment_mb, payment_c and invoice_issuer are mandatory if the user introduces a value greater than "0" for field registration_type_price.
But its not working, the validation message is not appearing with "required_if:registration_type_price,>,0'". Do you know why?
$rules = [
...
'payment_mb' => 'required_without:payment_c|required_if:registration_type_price,>,0',
'payment_c' => 'required_without:payment_mb|required_if:registration_type_price,>,0',
'invoice_issuer' => 'required_if:registration_type_price,>,0',
];
$customMessages = [
...
'payment_mb.required_if' => 'The field payment methods is mandatory.',
'payment_c.required_if' => 'The field payment methods is mandatory.',
'invoice_issuer.required_if' => 'The field invoice issuer is mandatory..'
];
$this->validate($request, $rules, $customMessages);
try this one
$("#personal_detail_form").validate({
rules: {
full_name: {
lettersonly: true
},phone_number : {
number : true,
minlength:10,
maxlength:10
},city : {
lettersonly : true
},pin_code : {
digits : true,
minlength:6,
maxlength:6
},

How to get common validation error message for array field

I need to validate a request with image and tags. How to return a common validation error message for tags.
$this->validate($request, [
'image' => 'required|image,
'tags.*' => 'string'
]);
Currecnt message is.
{
"image": [
"The image field is required."
],
"tags.0": [
"The tags.0 must be a string."
],
"tags.1": [
"The tags.1 must be a string."
]
}
Expected message is.
{
"image": [
"The image field is required."
],
"tags": [
"The tags must be a string."
]
}
Have you tried this,
$this->validate($request, [
'image' => 'required|image,
'tags.*' => 'string'
],$messages = [
'tags.*' => 'The tags must be a string.'
]);
I'm not quit sure but this might work for you.
I think you should try with similar below example:
public function messages()
{
$messages = [];
foreach ($this->request->get('tags') as $key => $val) {
$messages['tags.' . $key . '.string'] = 'The tags must be a string.'
}
return $messages;
}
Hope this work for you !!!
You can add messages with a * to your translation file, as documented.
'custom' => [
'tags.*' => [
'string' => 'The tags must be a string.',
]
],

How to get array index in validation message Laravel 5.2

These arrays I put into Laravel Validator as arguments:
['item.*' => 'string'] // rules
['item.*.string' => 'Item number (index) is not string'] // messages
I want to have index number in validation message. Code above is just for demonstration and does not work. How to do this?
Try this or use this one
'name' : [ { 'value' : 'raju' } , { 'value' : 'rani'} ]
and validate it by
'name.*' or 'name.*.value' => 'required|string|min:5'
The message will be
'name.*.required' => 'The :attribute is required'
'name.*.value.required' => 'The :attribute is required'
I think it will help to you..
Try this one,
public function messages()
{
$messages = [];
foreach ($this->request->get('name') as $key => $value){
$messages['name.'. $key .'.required'] = 'The item '. $key .' is not string';
}
return $messages;
}

Laravel 5.2 required_without_all Request Issue

I'm having an issue with required_without_all. I have three elements, and at least one should be filled. My file input's name is image[]. Adding an image but leaving title and body empty still results in a validation error, even though it shouldn't.
Any thoughts on what I'm doing wrong?
public function rules()
{
return [
'title' => 'required_without_all:body,image.*',
'body' => 'required_without_all:title,image.*',
'image.*' => 'required_without_all:body,title',
];
}
public function messages()
{
return [
'title.required_without_all' => 'At least one field is required',
'body.required_without_all' => 'At least one field is required',
'image.*.required_without_all' => 'At least one field is required',
];
}
Answered here: https://stackoverflow.com/a/39089295/2101328
Basically, add image as an array and remove the * from the rules.
There was also a bug in Laravel 5.3 that prevented similar from working; see this thread: https://github.com/laravel/framework/issues/15044#issuecomment-244364706
public function rules()
{
return [
'title' => 'required_without_all:body,image',
'body' => 'required_without_all:title,image',
'image' => 'array',
'image.*' => 'required_without_all:body,title',
];
}

Resources