1

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?

1

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You must put a validation on the product_id itself. E.g.

$validator = Validator::make($request->all(), [
   'considerations' => 'required',
   'product_id'  => "required|array",
   'product_id.*'  => "required"
]);

1 Comment

Your answer contains a great point .. I also need to check being array for product_id input. upvote
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.