简体   繁体   English

NelmioApiDocBundle如何在@ApiDoc(输入)注释中将参数传递给表单类型类?

[英]NelmioApiDocBundle how to pass a parameter to a form type classe in @ApiDoc (input) annotation?

I have a form used as a service which requires the service container as first param in constructor. 我有一个用作服务的表单,该表单要求服务容器是构造函数中的第一个参数。

# in my services.yml file
    acme_foo_bundle.fish.form.type:
    class: Acme\FooBundle\Form\Type\FishType
    arguments: [@service_container]
    scope: request

Both : form and REST API are working well but when I browse the api doc (/app_dev.php/api/doc) it throws an error 500. :form和REST API都可以正常工作,但是当我浏览api doc(/app_dev.php/api/doc)时,抛出500错误。

After investigations now I know that the error thrown because of the following lines in my rest controller : 经过调查后,我知道由于我的rest控制器中的以下几行而引发了错误:

 /**
 * in my rest controller file (postFishAction)
 * @ApiDoc(
 *   resource = true,
 *   input = "Acme\FooBundle\Form\Type\FishType",
 *   statusCodes = {
 *     200 = "Returned when successful",
 *     400 = "Returned when the form contains errors"
 *   }
 [...]
 */
 public function postFishAction(Request $request){ [...] 

The problem is this line : 问题是这一行:

   input = "Acme\FooBundle\Form\Type\FishType",

it requires the service container as param and I don't know how to give it throught the @ApiDoc annotation. 它需要将服务容器作为参数,并且我不知道如何通过@ApiDoc批注给它。

Thanks for your help ✌ 谢谢你的帮助✌

You should try with passing name of registered form type. 您应该尝试传递注册表单类型的名称。

As doc says: 正如文档所说:

input: the input type associated to the method (currently this supports Form Types, classes with JMS Serializer metadata, and classes with Validation component metadata) useful for POST|PUT methods, either as FQCN or as form type (if it is registered in the form factory in the container). 输入:与方法关联的输入类型(当前支持表单类型,具有JMS序列化器元数据的类以及具有验证组件元数据的类),对POST | PUT方法很有用,可以是FQCN或表单类型 (如果已在在容器中形成工厂)。 ``` ```

so you should try something like this: 所以您应该尝试这样的事情:

 /**
 * @ApiDoc(
 *   resource = true,
 *   input = "acme_foo_fish_type",
 *   statusCodes = {
 *     200 = "Returned when successful",
 *     400 = "Returned when the form contains errors"
 *   }
 [...]
 */

and your form type registration: 和您的表格类型注册:

services:
    acme_foo_bundle.fish.form.type:
        class: Acme\FooBundle\Form\Type\FishType
        arguments: [@service_container]
        scope: request
        tags:
            - { name: form.type, alias: acme_foo_fish_type }

and FishType must return acme_foo_fish_type in getName method. FishType必须在getName方法中返回acme_foo_fish_type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM