使用ParamFetcher进行验证非常优雅,但错误消息并不漂亮。它包含许多不必要的信息,我不想向api用户显示这些信息。
例如:
"查询参数parent_id值' a'违反约束(查询 参数值' a',与要求不匹配' \ d +')"
我想将此消息转换为更简单的消息,如:" parent_id必须是整数"
我该怎么做?
出现错误的代码如下。而且我没有看到任何方式提供其他错误消息。
可能还有另一种验证查询参数的替代方法。
$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
答案 0 :(得分:2)
我向FOSRestBundle发出拉取请求以解决此问题https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1015
您可以使用它,如下所示
@QueryParam(name="parent_id", requirements={"rule" = "\d+", "error_message" = "parent_id must be an integer"}, strict=true, nullable=true, description="Parent Id")
错误讯息:
{
"code": 400,
"message": "parent_id must be an integer"
}