【问题标题】:Symfony QueryParam with empty value空值的 Symfony QueryParam
【发布时间】:2017-08-02 08:23:22
【问题描述】:

我正在尝试在我的 symfony 控制器上使用 QueryParam。

我不想接受查询中的空参数的问题。但是当我用

定义QueryParam
@REST\QueryParam(name="test" , description="The test", requirements="\w+", strict=true, nullable=false)

我尝试使用mysite.com/test= 调用网络服务,它仍在工作。

如果参数为空,我必须做些什么来拒绝请求中的空参数而不在代码中进行测试

谢谢

【问题讨论】:

    标签: symfony controller queryparam


    【解决方案1】:

    当测试足够奇怪时,验证似乎不适用于配置param_fetcher_listener,只是在true 上,但是当我使用force 时,一切都突然起作用了:

    # config.yml
    fos_rest:
        param_fetcher_listener: force
    

    在控制器中:

    /**
     * @QueryParam(name="test" , description="The test", requirements="[a-zA-Z]+", strict=true, nullable=false, allowBlank=false)
     */
    public function indexAction($test)
    {
        // replace this example code with whatever you need
        return new JsonResponse($test);
    }
    

    请注意,在我的示例中,有多个验证,只有 ?test=a 可以工作。不,test?test=?test=1 都将因特定原因而不起作用。

    如果有人知道如何让它与 param_fetcher_listener: true 一起工作,我很好奇 :)

    另请参阅文档 (https://symfony.com/doc/master/bundles/FOSRestBundle/param_fetcher_listener.html),了解使用 force 的含义

    【讨论】:

    • 感谢您的回答,当我尝试添加 'allowBlank=false' 时出现错误,我正在使用 symfony 2.8
    • 终于成功了,我将 /rest-bundle 更新为 1.5 :) 一切正常 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    相关资源
    最近更新 更多