I want to access information within nested validation array to do a custom validation, here is my code:
return [
...,
'amount' => ['nullable', 'numeric'],
'currency_id' => ['nullable', 'integer', Rule::in($allowedCurrencyIds)],
'details' => ['nullable'],
'details.*.product_id' => ['required', 'integer', 'exists:products,id'],
'details.*.product_collection_id' => [
'nullable',
'integer',
'exists:product_collections,id',
new ProductAndCollectionValidator($this->details.*.->product_id, $this->details->product_collection_id)
],
...
]
As you can see I want to access the product id and the product collection id with details.* to send to the custom validatior ProductAndCollectionValidator. And I need to concider that product_collection_id might be null sometimes. And that is the first step.
Second step I want to make sure that there are no duplicate product_id and product_collection_id the the details array
How can I do that?
2
Answers
you can use Laravel’s custom validation rules and validation closures. https://laravel.com/docs/11.x/validation#using-closures. Try this on your validator:
Let me know. Cheers.
Here are 2 solutions. I didn’t tested. Please let me know if there is an error.
Request:
Rule:
Request:
Rule: