Symfony - 验证空查询参数值

时间:2014-07-08 15:26:14

标签: php symfony fosrestbundle query-parameters

我正在使用FOSRestBundle,并且想知道是否可以使用注释验证空查询参数?

例如,在调用:/comments/1时会抛出异常,因为尚未设置dealIdsource个查询参数。

然而,即使/comments/1?dealId=1&source=值没有设置并且与注释中列出的正则表达式不匹配,调用source也没问题。

控制器功能:

/**
 * Get a single comment.
 *
 * @Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, description="The deal the comments belong to.")
 * @Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, description="The source of the comments.")
 *
 * @Annotations\View()
 *
 * @Annotations\Get("/comments/{id}", requirements={"id" = "\d+"})
 *
 */
public function getCommentAction(Request $request, ParamFetcherInterface $paramFetcher, $id)
{
    $dealId = $paramFetcher->get('dealId');
    $source = $paramFetcher->get('source');

    // TODO: Implement


    return [ 'id' => $id, 'dealId' => $dealId, 'source' => $source ];
}

更新

我也在FOSRestBundle的GitHub回购中提出了这个问题,由于正在使用的正则表达式验证器的限制,看起来我目前无法提出要求。

https://github.com/FriendsOfSymfony/FOSRestBundle/issues/814#issuecomment-49696288

5 个答案:

答案 0 :(得分:2)

只需使用QueryParam的allowBlank选项即可。在您的情况下,您可以将allowBlank设置为false以获得预期的行为:

在FOSRestBundle中,allowBlank选项不是YET,但是我为FOSRestBundle提供了一个补丁,它很有可能在下一个版本中登陆,版本1.5.0

这就是您的控制器的样子:

/**
 * Get a single comment.
 *
 * @Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, description="The deal the comments belong to.")
 * @Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, allowBlank=false, description="The source of the comments.")
 *
 * @Annotations\View()
 *
 * @Annotations\Get("/comments/{id}", requirements={"id" = "\d+"})
 *
 */
public function getCommentAction(Request $request, ParamFetcherInterface $paramFetcher, $id)
{
    $dealId = $paramFetcher->get('dealId');
    $source = $paramFetcher->get('source'); 
}

答案 1 :(得分:2)

如果要强制检查参数,可以按照文档中的说明更改配置文件,以下是示例:

fos_rest: param_fetcher_listener: force

然后你可以设置其他选项,如strict,nullable。

在此处查看更多详情:

http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html

答案 2 :(得分:1)

@Annotations \ QueryParam期望在使用nullable参数时设置strict参数(true或false)。尝试设置它。

我想你想要:

@Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, nullable=false, description="The deal the comments belong to.")
@Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, nullable=false, description="The source of the comments.")

另请阅读docs中有关QueryParam的更多信息。

答案 3 :(得分:1)

棘手的部分是允许sourcedealId为空但我认为可能是 将这些参数添加到您的路由(因此必须指定它们才能访问控制器)并为每个参数使用字符串前缀(即dealid_source_),这样就可以指定一个空值

您还需要修改正则表达式要求以允许空值。

/**
 * Get a single comment.
 *
 * @Annotations\View()
 * @Annotations\Get("/comments/{id}/dealid_{dealId}/source_{source}", 
 *    requirements={"id" = "\d+", "dealId" = "\d*", "source" = "(forum|blog)*"})
 */
public function getCommentAction(Request $request, 
    ParamFetcherInterface $paramFetcher, $id, $dealId, $source) 
{
    return [ 'id' => $id, 'dealId' => $dealId, 'source' => $source ];
}

答案 4 :(得分:-3)

我不熟悉symfony,但我认为这很简单

$dealId = isset($dealId) ? $dealId : '';

会帮助您解决问题

相关问题