2015-10-19 90 views
6

我使用input属性@ApiDoc注释来指定my api的参数,它们是表单的字段。如何在Nelmio中指定参数的格式ApiDocBundle

* @ApiDoc(
*  section="User", 
*  resource=true, 
*  input={ 
*   "class"="Nik\UserBundle\Form\UserType", 
*  }, 
*  .... 

data_class表单是一个对属性进行约束验证的实体。

我希望nelmio api doc指定参数格式为实体的验证约束,但格式为空。

enter image description here

我怎么能在nelmio ApiDocBundle指定参数格式?


编辑: 也许我写不好的问题。

我们可以为input & output指定解析器,如果我们没有指定这些分析器,它调用所有解析器input & output,那么所有的语法被称为为UserType

nelmio已经解析器命名ValidationParser有一个方法叫parseConstraint,对于输入输出&设置format,但这种方法不叫我的文档,为什么呢?

回答

2

我提交使用format属性的验证元数据的拉取请求。

你可以看到这个PRhere

0

正如你所看到的here你可以在注释中指定过滤器,就像它完成文档示例一样。

本实施例的在此部分:

/** 
* This is the documentation description of your method, it will appear 
* on a specific pane. It will read all the text until the first 
* annotation. 
* 
* @ApiDoc(
* resource=true, 
* description="This is a description of your API method", 
* filters={ 
*  {"name"="a-filter", "dataType"="integer"}, 
*  {"name"="another-filter", "dataType"="string", "pattern"="(foo|bar) ASC|DESC"} 
* } 
*) 
*/ 
public function getAction() 
{ 
} 
+0

我没有想写入筛选的手,我会用我的形式输入和nelmio阅读形式与实体验证,并使用这些作为格式参数 – ghanbari

5

格式列仅对datetimedatechoice类型设计的。

对于datetimedate它表示日期格式,如Y-m-d H:i:schoice的选项阵列。

我还没有找到任何有关它的文档,所以我不得不查看源代码。这是FormTypeParser类,其中FormType实际上已被解析,并且您可以看到如何设置格式字段。

FormTypeParserTest类,你可以看到如何使用它。只需传递字符串参数format名称作为其中一种可用类型,解析器将处理它。

更新:你要在你的FormType类中定义你的约束。

例如:

class TestType extends AbstractType 
{ 
    /** 
    * @Assert\Type("string") 
    * @Assert\Length(min="10", max="255") 
    * @Assert\Regex("/^[^<>]+$/i") 
    */ 
    private $title; 

    /** 
    * @Assert\Type("string") 
    * @Assert\Length(min="10", max="255") 
    * @Assert\Regex("/^[^<>]+$/i") 
    */ 
    private $content; 

    /** 
    * @Assert\Date() 
    */ 
    private $created; 

    public function getName() 
    { 
     return 'test'; 
    } 
} 

将被分解成:

doParse()方法enter image description here

ValidationParser发现您的FormType类中定义的所有约束,然后执行为他们每个人parseConstraint()方法。

您也可以按照上面所述使用FormTypeParser。例如:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('created', 'date', array('label' => 'Created', 'format' => 'yyyy-MM-dd')) 
     ->add('color', 'choice', array('label' => 'Color', 'choices' => array('grey' => '#CCCCCC', 'red' => '#FF0000'))) 
     ->add('save', 'submit'); 
} 

会被解析为:

enter image description here

希望它现在帮助!

+0

请参阅我的问题,我更新它 – ghanbari

+0

@ghanbari更新了答案 – chapay

+0

谢谢你,它的工作,但在第一种方式,我强制重新定义表单类型中的所有实体属性是不是有用的,在第二种方式,我不能指定其他字段的格式为文本 – ghanbari

相关问题