I'm using Respect Validation to attempt to validate data sent via a POST request. In doing so I discovered an issue where an exception in the Respect Validation library is not being caught.
$postValidator = v::key('name', v::stringType()->length(3, 50))
->key('email', v::email())
->key('contact', v::phone())
->key('message', v::stringType()->length(7, 1000));
try {
$isValid = $postValidator->assert(['random' => 'thing']);
} catch(Exception $exception) {
return $response->withJson([
'success' => false
]);
}
Testing the following returns the following error:
All of the required rules must pass for { "random": "thing" }
Here I've used a generic Exception
that should catch any exception that occurs within the assert
method. But it appears it doesn't and I'm getting a generic Slim error, instead of a JSON response:
Any ideas why I cannot catch that exception?
The problem I am having is Respect Validation will throw an exception if a naughty users was to input postdata that the app can not handle. Personally I'd rather respect validation just ignore those, but it doesn't. So I want to catch the exception that Respect throws when a user passes undefined post data and return a response. Except, in the code above the exception isn't being caught.