How to specify parameter format in Nelmio ApiDocBundle

I use the input property of the @ApiDoc annotation to specify the parameters of my api, which are the fields of the form.

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

form data_class is an object that has a constraint check for properties.

I expect the nelmio api doc to define the parameter format as entity check constraints, but the format is empty.

enter image description here

How can I specify parameter formats in nelmio ApiDocBundle?


EDIT : maybe I'll write a bad question.

we can specify parsers for input and output , if we do not specify a parser for them, it calls the entire parser for input and output , then the entire parser is called for UserType .

nelmio has a parser named ValidationParser that has a method called parseConstraint that sets the format for input and output, but this method is not called for my document, why?

+6
source share
3 answers

i send a transfer request that uses validation metadata for the format property.

you can see this PR here

+2
source

The format string is for datetime , date and choice types only.

For datetime and date it represents a date format, for example Ymd H:i:s , and an array of options for choice .

I did not find any documentation about this, so I had to look at the source code. This is the FormTypeParser class, the place where FormType actually parsed, and you can see how the format field is set.

In the FormTypeParserTest class , you can see how to use it. Just pass a string parameter called format for one of the available types, and the analyzer will process it.

UPDATE:. You must define your constraints within your FormType class.

For instance:

 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'; } } 

will be analyzed for:

enter image description here

ValidationParser in the doParse () method finds all the restrictions defined in your FormType class, and then executes the parseConstraint() method for each of them.

You can also use FormTypeParser , as I described above. For instance:

 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'); } 

will be analyzed as:

enter image description here

Hope this helps now!

+5
source

As you can see here , you can specify the filter in the annotation, as the sample documentation did.

Here is part of this example:

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

All Articles