Validation with ParamFetcher is very elegant but error messages is not pretty. It contains many unnecessary information which I don't want to show to api users.
For example:
"Query parameter parent_id value 'a' violated a constraint (Query parameter value 'a', does not match requirements '\d+')"
I want to convert this message to more simple message like: "parent_id must be an integer"
How can I do it?
Code which gives an error is below. And I don't see any way to give another error messages.
Maybe there is an another alternative for validation query parameters.
$constraint = new Regex(array(
'pattern' => '#^'.$config->requirements.'$#xsu',
'message' => sprintf(
"%s parameter value '%s', does not match requirements '%s'",
$paramType,
$param,
$config->requirements
),
));
Source Code: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Request/ParamFetcher.php#L220
I made a pull request to FOSRestBundle to solve this problem https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1015
You can use it as shown below
@QueryParam(name="parent_id", requirements={"rule" = "\d+", "error_message" = "parent_id must be an integer"}, strict=true, nullable=true, description="Parent Id")
Error Message:
{
"code": 400,
"message": "parent_id must be an integer"
}