【发布时间】:2015-06-18 22:20:44
【问题描述】:
使用 ParamFetcher 进行验证非常优雅,但错误消息并不漂亮。它包含许多我不想向 api 用户显示的不必要信息。
例如:
"查询参数 parent_id 值 'a' 违反了约束(查询 参数值'a',不符合要求'\d+')"
我想将此消息转换为更简单的消息,例如:“parent_id must be an integer”
我该怎么做?
给出错误的代码如下。而且我看不到任何方法可以提供其他错误消息。
也许还有另一种验证查询参数的替代方案。
$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
【问题讨论】:
-
如果我记得,您可以在
requirements部分使用自定义约束。试试你自己的Regex类,前缀为'@'
标签: php validation api symfony fosrestbundle