php - Symfony - 验证空查询参数值

标签 php symfony fosrestbundle query-string

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

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

但是,即使 source 值尚未设置并且与中概述的正则表达式不匹配,调用 /comments/1?dealId=1&source= 也可以。注释。

Controller 功能:

/**
 * 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

最佳答案

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

fos_rest:param_fetcher_listener:强制

然后您可以相应地设置其他选项,例如 strict、nullable。

在此处查看更多详细信息:

http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html (archive.org) https://symfony.com/doc/3.x/bundles/FOSRestBundle/index.html#config-reference https://symfony.com/doc/3.x/bundles/FOSRestBundle/annotations-reference.html

关于php - Symfony - 验证空查询参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24635776/

相关文章:

rest - 在 Symfony 的 REST api 中创建链接实体

rest - 在 FOSRestBundle 中使用 Symofny Forms 处理 POST 请求

javascript - 如何使用 javascript 添加一个文本框,其中包含在数据库中检索到的引用计数?

php - 从数组范围搜索值

php - Symfony Controller 中的 json_decode ?

symfony - 如何修复 Gedmo sortable 中的 "This repository can be attached only to ORM sortable listener"错误?

php - 如何使用 JMS Serializer 在 FOS Rest Bundle 中的 JSON 中显示空值?

php - 如何在 Laravel 5.8 中为 YouTube 设置 SameSite cookie

php - 从 MySQL 中的 DateTime 计算不同的天数

symfony - TWIG:已定义且不为空