Fosrestbundle: How to change error message given by ParamFetcher

Created on 13 Apr 2015  路  7Comments  路  Source: FriendsOfSymfony/FOSRestBundle

Validation with ParamFetcher is very elegant but error messages is not pretty

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?

$constraint = new Regex(array(
         'pattern' => '#^'.$config->requirements.'$#xsu',
         'message' => sprintf(
                    "%s parameter value '%s', does not match requirements '%s'",
                    $paramType,
                    $param,
                    $config->requirements
         ),
));

https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Request/ParamFetcher.php#L220

I asked a question about this subject on Stackoverflow http://stackoverflow.com/questions/29608164/how-to-change-an-error-message-given-by-paramfetcher

All 7 comments

One could make the message configurable though we should then likely name the sprintf parameters.

I will do it if it ok for community

as long as it can be done without a big refactoring I am open to this.

@stof or do you have another idea how to do this? maybe via the translation system?

Example Usage:

@QueryParam(name="parent_id", requirements={"rule" = "\d+", "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"
}

Hello!
Now there are different options for messages like minWidthMessage, minPixelsMessage and so on.
But when using FOSRestBundle, validation message returns in very ugly way:

"Parameter \"document\" of value \"Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(\n 'test' => false,\n 'originalName' => 'shedevr.png',\n 'mimeType' => 'application/octet-stream
',\n 'error' => 0,\n))\" violated a constraint \"The image width is too small (128px). Minimum width expected is 500px.\""

And it's not catchable.
Does anybody know how to return just a text message "The image width is too small (128px). Minimum width expected is 500px." without technical info?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaksSlesarenko picture MaksSlesarenko  路  4Comments

soullivaneuh picture soullivaneuh  路  5Comments

matrix818181 picture matrix818181  路  7Comments

andrzej-pakula picture andrzej-pakula  路  4Comments

timotheemoulin picture timotheemoulin  路  4Comments