gha*_*ari 6 symfony symfony-2.7 nelmioapidocbundle
我使用注释的input属性@ApiDoc来指定我的api的参数,这些参数是表单的字段.
* @ApiDoc(
* section="User",
* resource=true,
* input={
* "class"="Nik\UserBundle\Form\UserType",
* },
* ....
Run Code Online (Sandbox Code Playgroud)
data_class of form是一个对属性进行约束验证的实体.
我希望nelmio api doc将参数格式指定为实体的验证约束,但格式为空.
如何在nelmio ApiDocBundle中指定参数格式?
编辑:也许我写了一个糟糕的问题.
我们可以为input&指定解析器output,如果我们没有为这些指定解析器,它会为input&调用所有解析器output,然后调用所有解析器UserType.
nelmio有一个名为ValidationParser的解析器,它有一个名为parseConstraint的方法,format用于输入和输出,但是这个方法没有为我的文档调用,为什么?
格式列只为设计datetime,date和choice类型.
对于datetime和date它表示日期格式Y-m-d H:i:s和选择数组choice.
我还没有找到任何关于它的文档,所以我不得不查看源代码.这是FormTypeParser类,FormType实际解析的位置,您可以看到格式字段的设置方式.
在FormTypeParserTest类中,您可以看到如何使用它.只需format为其中一个可用类型传递带有name 的字符串参数,解析器就会处理它.
更新:您要在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';
}
}
Run Code Online (Sandbox Code Playgroud)
将被解析为:
ValidationParser在
doParse()方法中查找在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');
}
Run Code Online (Sandbox Code Playgroud)
将被解析为:
希望现在有所帮助!
| 归档时间: |
|
| 查看次数: |
7245 次 |
| 最近记录: |