How to set required_if in laravel 7 - laravel

I have two textboxes color_name[] and color_code[],
How to put required_if? If the color code is not empty, the color name is required.
'color_name' => 'required_if:color_code',
error
Validation rule required_if requires at least 2 parameters.

$validator = Validator::make($request->all(), [
'color_name' => 'required_with:color_code',
]);
if ($validator->fails()) {
return $validator->errors()->first();
}
Use this to solve the problem.
For array please re shape your array into this
array:2 [
0 => array:2 [
"color_name" => "red"
"color_code" => "#77"
]
1 => array:2 [
"color_name" => "blue"
"color_code" => "#88"
]
]
$validator = Validator::make($request->all(), [
'color'=>'array',
'color.*.color_name' => 'required_with:color.*.color_code',
]);

Related

Validating Arrays not working in laraval 9

Validating Arrays not working in laraval 9.
Request as follows
array:2 [
0 => array:4 [
"nic" => "908110248V"
"employee_id" => "1"
"request_id" => "2"
"schedule_training_id" => "1"
]
1 => array:4 [
"nic" => "962930898v"
"employee_id" => "2"
"request_id" => "1"
"schedule_training_id" => "1"
]
]
validator code snipit as follows
$validator = Validator::make($request->input('data_attributes'), [
'data_attributes.*.nic' => 'required|max:9'
]);
$validator = Validator::make($request->input('data_attributes'), [
'*.nic' => 'required|max:9'
]);
You should check the matrix as a whole and then start working with its elements, this is an example of what you should do:
$validator = Validator::make($request->input('data_attributes'), [
"data_attributes" => "required|array|min:3",
"data_attributes.*" => "required|string|distinct|min:3",
]);

How to write validation rule for JSON laravel?

There is JSON object that I want to validate:
[{
"id": 1,
"settings": {
"GRSYSEM": 1
}
},
{
"id": 2,
"settings": {
"GRSYSEM": 1
}
},
{
"id": 3,
"settings": {
"GRSYSEM": 1
}
}
]
How to write validation rule in Laravel?
I tried this rule:
$validator = Validator::make($request->all(), [
'id' => 'required|array',
'id.*' => 'required',
'settings.*.GRSYSEM' => 'required'
]);
You are almost there, simply put the wildcard * first:
$validator = Validator::make($request->all(), [
'*.id' => 'required',
'*.settings.GRSYSEM' => 'required'
]);
It literally says: For each element in the array, I expect an id and a setting GRSYSEM.
You could also ensure it's an array by using a little hack:
$data = ['input' => $request->all()];
$validator = Validator::make($data, [
'input' => 'required|array',
'input.*.id' => 'required',
'input.*.settings.GRSYSEM' => 'required'
]);
If entry in $request->all() is id (as I can see), you should try like that :
$validator = Validator::make($request->all(), [
'id' => 'required|array',
'id.*.id' => 'required',
'id.*.settings.GRSYSEM' => 'required'
]);

Elasticsearch one regexp for multiple fields

I'm new to Elasticsearch. I need to write query to find regexp match in one of the fields.
If I'm looking for regexp in one field everything works fine (PHP code):
$data = ['body' => ['query' => ['regexp' => ['abstract' => ".*searchtext.*"]]]];
what to do if I want to find documents in which at least one field satisfies regexp?
This query:
$data = [
'body' => [
'query' => [
'multi_match' => [
'query' => 'searchtext',
'fields' => [
'type',
'title',
'abstract',
'body_text'
]
]
]
]
];
only finds documents with whole word "searchtext" match.
Regards, Tomas
Found answer:
$data = [
'body' => [
'query' => [
'bool' => [
'should' => [
['regexp' => ['type' => ".*searchtext.*"]],
['regexp' => ['title' => ".*searchtext.*"]],
['regexp' => ['abstract' => ".*searchtext.*"]],
['regexp' => ['body_text' => ".*searchtext.*"]],
]
]
]
]
];
not so elegant as multi_match query, but works.

Laravel 5.6 Validation Array Data

When validating a request data, we are getting the result filtered out by the rules. For example, the following request:
$this->post(route('test'), [
'foo' => 'bar',
'john' => 'doe'
]);
And in my controller, I have the following:
$data = request()->validate([
'foo' => 'required|string'
]);
Then if I perform a dd($data);, I would get the following:
array:1 [
"foo" => "bar"
]
So far so good. However, when working with arrays, for example, the following request:
$this->post(route('test'), [
'foo' => 'bar',
'names' => [
'en' => 'Product',
'fr' => 'Produit',
'john' => 'Doe'
]
]);
And then in my controller, I have the following:
$data = request()->validate([
'foo' => 'required|string',
'names' => 'array',
'names.en' => 'required|string',
'names.fr' => 'required|string'
]);
It would return the following $data:
array:2 [
"foo" => "bar"
"names" => array:3 [
"en" => "Product"
"fr" => "Produit"
"john" => "doe"
]
]
Now my questions are:
Why is 'john' => 'doe' in my names key when I don't have it in my validation rules?
Is there a way to remove unspecified array keys from the data?
Update
I've tested the suggestion of #JonasStaudenmeir, however the result is not what I expected.
Request:
$this->post(route('test'), [
'names' => [
'en' => 'Doe',
'fr' => 'John'
]
]);
Controller:
$rules = [
'names' => 'array',
'names.en' => 'string'
];
dd(request()->only(array_keys($rules));
Expected Output:
array:1 [
"names" => array:2 [
"en" => "Doe"
]
]
Actual Output:
array:1 [
"names" => array:2 [
"fr" => "John"
"en" => "Doe"
]
]
For cases with only nested rules ('names.en') but no parent rule ('names'),
this will be fixed in 5.7: https://github.com/laravel/framework/pull/23708
Until then you can use this workaround:
$data = request()->only(array_keys($rules));
If you do you have a parent rule, this would be much more complicated to achieve.
I would argue that receiving the whole 'names' array in the validated data can be seen as the expected behavior. The idea is that the validator returns the data for each rule. In your case it returns the data for the 'names' => 'array' rule, which is the whole 'names' array.

Laravel validation required_without_all

Call me stupid, but I can't get it :)
I want to set up validator rule, so it will pass only if one of the two fields is present (adgroup or all_adgroups).
Here is my controller:
$this->validate($request,
[
'new_target_cpa_value' => 'required|numeric',
'adgroups' => 'exists:google.ad_groups,id|required_without_all:all_agroups',
'all_agroups' => 'required_without_all:adgroups'
]
);
dd($request->all());
Here is dd:
"_token" => "aHjluUXPuZpEbglmVt4UePhriGvRWDOjk3OgfF88"
"new_target_cpa_value" => "123"
"adgroups" => array:1 [▶]
"all_agroups" => "1"
Try this:
$this->validate($request,
[
'new_target_cpa_value' => 'required|numeric',
'adgroups' => 'exists:google.ad_groups,id',
'all_agroups' => 'required_unless:adgroups,null'
]
);

Resources