Here is my validator:
$validator = Validator::make($request->all(), [
'considerations' => 'required',
'product_id.*' => "required"
]);
It validates considerations
as well. If $request
be lack of considerations
item, it throws:
The considerations field is required.
Which is right. But it doesn't care about product_id
. If I don't send that parameter it passes the validation as well. It will throw an error only if I send product_id[0]
and no value for it. Then it will throw:
The product_id.0 field is required.
But if I don't send product_id
parameter, it doesn't care and doesn't throw any error. Why? And how can I fix it?
Looks as though your validation rules are equivalent to saying
"Ensure any contents of the product_id array hold a value if the product_id field is provided"
I would add another rule which is 'product_id' => 'required'
to your validation array which should ensure that the product_id key is in the post fields too.
You must put a validation on the product_id
itself. Eg
$validator = Validator::make($request->all(), [
'considerations' => 'required',
'product_id' => "required|array",
'product_id.*' => "required"
]);
You can use like this
$rules = ['considerations' => 'required',
'product_id-token' => 'required|array'];
$message = ['considerations.required' => 'Your validation message',
'product_id-token.required' => 'Your validation message',];
$validator = Validator::make($request->input(), $rules, $message);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.