php – Symfony – 验证空查询参数值
发布时间:2020-12-13 13:47:11 所属栏目:PHP教程 来源:网络整理
导读:我正在使用FOSRestBundle,并且想知道是否可以使用注释验证空查询参数? 例如,当调用:/ comments / 1时抛出异常,因为dealId和源查询参数都未设置. 即使源值没有设置并且与注释中概述的正则表达式不匹配,但是调用/ comments / 1?dealId = 1 source =很好. 控
我正在使用FOSRestBundle,并且想知道是否可以使用注释验证空查询参数?
例如,当调用:/ comments / 1时抛出异常,因为dealId和源查询参数都未设置. 即使源值没有设置并且与注释中概述的正则表达式不匹配,但是调用/ comments / 1?dealId = 1& source =很好. 控制器功能: /** * Get a single comment. * * @AnnotationsQueryParam(name="dealId",requirements="d+",strict=true,description="The deal the comments belong to.") * @AnnotationsQueryParam(name="source",requirements="(forum|blog)",description="The source of the comments.") * * @AnnotationsView() * * @AnnotationsGet("/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回购中提出了这个问题,看起来我正在要求的是由于正在使用的Regex验证器的限制,目前是不可能的. https://github.com/FriendsOfSymfony/FOSRestBundle/issues/814#issuecomment-49696288
只需使用QueryParam的allowBlank选项.在您的情况下,您将设置allowBlank为false以获得预期的行为:
在FOSRestBundle中,allowBlank选项不是YET,但是我为FOSRestBundle提供了一个补丁,这个补丁很有可能在下一个版本1.5.0的版本中登陆. 这是你的控制器的外观: /** * Get a single comment. * * @AnnotationsQueryParam(name="dealId",allowBlank=false,$id) { $dealId = $paramFetcher->get('dealId'); $source = $paramFetcher->get('source'); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |