How to validate that fruits from a given $list are in the allowed list ?
$attributes [
"fruits" => ['nullable', 'string', 'in:apple,banana,orange'],
];
$list = [
"fruits" => [
"apple",
"orange"
]
];
$validator = Illuminate\Support\Facades\Validator::make($list, $attributes);
you can validate each item in this array individually:
$attributes = [
"fruits.*" => ['nullable', 'string', Rule::in(['apple','banana','orange'])],
];
Related
I try to perform a manual validation according to the documentation.
There is my rules:
$rules = [
"firstname" => ['required'],
"coord.address" => ['required'],
];
And there is my custom messages:
$messages = [
"firstname.required" => "Custom message for firstname",
"coord.address.required" => "Custom message for address",
];
My call for perform the validation
$errorMessages = Validator::make($attributes, $rules, $messages)->errors()->messages();
And there is my messages form the validator:
[
"firstname" => "Custom message for firstname",
"coord.address" => "The field coord.address is required",
]
What im doing wrong ?
I tried to use an array of messages like this:
$messages = [
"coord" => [
"address.required" => "Custom message",
]
];
Or like this:
$messages = [
"coord" => [
"address" => [
"required" => "Custom message"
]
]
];
But same problem.
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',
]);
[
"bob" => $books
"maria" => $otherBooks
]
to
[
[
name => "bob"
data => $books
],
[
name => "maria"
data => $otherBooks
]
]
You can utilize map, map takes $item and $key there is your input arrays key value, as the argument for the closure. If you map those to expected array structure this should work.
$result = collect([
'bob' => $books
'maria' => $otherBooks
])->map(function ($item, $key) {
return [
'name' => $key,
'data' => $item,
]
});
You can do this easily with collections.
collect([
"bob" => $books
"maria" => $otherBooks
])->map(function ($books, $name) {
return [
'name' => $name,
'data' => $books
];
});
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'
]);
I'm trying to find a way where I can build my $params array using the native syntax to search for a post which name is equal to 'test':
$params = [
"index" => "post_index",
"type" => "post",
'body' => [
'query' => [
'match' => [
'name' => 'test'
]
]
]
];
$result = $finder->find($params);
Unfortunately I'm getting this error:
Unknown key for a VALUE_STRING in [index].
I know that I can use some Elastic\Query\Match to build my query.
Just I fixed my $params array this way :
$q = [
'query' => [
'match' => [
'title' => 'test'
]
]
];