I've created a custom FormRequest
to validate requests relating to a particular product with attributes. One of the rules is, of course, that the request product
field exists in the products
database.
However, I have a more complicated rule that must be checked after. I'm checking this rule using the withValidator
method, and then doing:
$validator->after(function ($validator) {
// check the extra rule
...
});
(It doesn't particularly matter, but this rule is to check that, given the user selected attributes like color and size, this corresponds to a unique sku belonging to the product)
So how do I cause the validation to fail?
Another requirement is that when this validation fails, it should return a 500 instead of a 422. Additionally, it should return a custom JSON response if this sku is out of stock. How do I do this within FormRequest?