Suppose we have the following form data:
{age: 15, father: "John Doe"}
We want to have some complex logic behind validating the father field based on other data for the same item (for this example, we want to validate that father has at least 5 characters when age < 18).
This could be done like that:
Standard validation rules: ['age': 'required|integer']
$validator->sometimes('father', 'required|min:5', function($data) {
return $data['age'] < 18;
});
Now, we want to have the same kind of thing with a list of items. So now, we have the following form data:
[
{age: 25, },
{age: 15, father: "John Doe"},
{age: 40, },
]
Common validation rule now looks like that:
['items.*.age': 'required|integer']
My issue is now to easily express the sometimes rule for each item's father field which will depend on the item's age field.
$validator->sometimes('items.*.father', 'required|min:5', function($data) {
// Does not work anymore: return $data['age'] < 18;
// Any way to know which item we are dealing with here?
});
One way I could think of is to loop over the items in a validator after callback. But that does not seem very elegant :(