mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Added a way to comment each field of a Form Type
This commit is contained in:
parent
f24ab9715e
commit
8b018e6de5
54
Form/Extension/DescriptionFieldTypeExtension.php
Normal file
54
Form/Extension/DescriptionFieldTypeExtension.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle.
|
||||||
|
*
|
||||||
|
* (c) Nelmio <hello@nelm.io>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Form\Extension;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\AbstractTypeExtension;
|
||||||
|
use Symfony\Component\Form\FormBuilder;
|
||||||
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
use Symfony\Component\Form\FormView;
|
||||||
|
|
||||||
|
class DescriptionFieldTypeExtension extends AbstractTypeExtension
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function buildForm(FormBuilder $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder->setAttribute('description', $options['description']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function buildView(FormView $view, FormInterface $form)
|
||||||
|
{
|
||||||
|
$view->set('description', $form->getAttribute('description'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getDefaultOptions()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'description' => '',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getExtendedType()
|
||||||
|
{
|
||||||
|
return 'field';
|
||||||
|
}
|
||||||
|
}
|
@ -56,7 +56,12 @@ class MarkdownFormatter extends AbstractFormatter
|
|||||||
foreach ($data['parameters'] as $name => $parameter) {
|
foreach ($data['parameters'] as $name => $parameter) {
|
||||||
$markdown .= sprintf("%s:\n\n", $name);
|
$markdown .= sprintf("%s:\n\n", $name);
|
||||||
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
$markdown .= sprintf(" * type: %s\n", $parameter['dataType']);
|
||||||
$markdown .= sprintf(" * is_required: %s\n", $parameter['required'] ? 'true' : 'false');
|
$markdown .= sprintf(" * required: %s\n", $parameter['required'] ? 'true' : 'false');
|
||||||
|
|
||||||
|
if (isset($parameter['description']) && !empty($parameter['description'])) {
|
||||||
|
$markdown .= sprintf(" * description: %s\n", $parameter['description']);
|
||||||
|
}
|
||||||
|
|
||||||
$markdown .= "\n";
|
$markdown .= "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ class FormTypeParser
|
|||||||
* Returns an array of data where each data is an array with the following keys:
|
* Returns an array of data where each data is an array with the following keys:
|
||||||
* - dataType
|
* - dataType
|
||||||
* - required
|
* - required
|
||||||
|
* - description
|
||||||
*
|
*
|
||||||
* @param AbstractType $type
|
* @param AbstractType $type
|
||||||
* @return array
|
* @return array
|
||||||
@ -66,7 +67,8 @@ class FormTypeParser
|
|||||||
|
|
||||||
$parameters[$name] = array(
|
$parameters[$name] = array(
|
||||||
'dataType' => $bestType,
|
'dataType' => $bestType,
|
||||||
'required' => $b->getRequired()
|
'required' => $b->getRequired(),
|
||||||
|
'description' => $b->getAttribute('description'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
README.md
21
README.md
@ -96,6 +96,27 @@ parameters, and you can document them as you want, but keep in mind to be consis
|
|||||||
If you set a `formType`, then the bundle automatically extracts parameters based on the given type,
|
If you set a `formType`, then the bundle automatically extracts parameters based on the given type,
|
||||||
and determines for each parameter its data type, and if it's required or not.
|
and determines for each parameter its data type, and if it's required or not.
|
||||||
|
|
||||||
|
You can add an extra option named `description` on each field:
|
||||||
|
|
||||||
|
``` php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class YourType extends AbstractType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function buildForm(FormBuilder $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder->add('note', null, array(
|
||||||
|
'description' => 'this is a note',
|
||||||
|
));
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
The bundle will also get information from the routing definition (`requirements`, `pattern`, etc), so to get the
|
The bundle will also get information from the routing definition (`requirements`, `pattern`, etc), so to get the
|
||||||
best out of it you should define strict _method requirements etc.
|
best out of it you should define strict _method requirements etc.
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="nelmio_api_doc.extractor.api_doc_extractor.class">Nelmio\ApiDocBundle\Extractor\ApiDocExtractor</parameter>
|
<parameter key="nelmio_api_doc.extractor.api_doc_extractor.class">Nelmio\ApiDocBundle\Extractor\ApiDocExtractor</parameter>
|
||||||
|
<parameter key="nelmio_api_doc.form.extension.description_field_type_extension.class">Nelmio\ApiDocBundle\Form\Extension\DescriptionFieldTypeExtension</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
@ -12,6 +13,9 @@
|
|||||||
<argument type="service" id="router" />
|
<argument type="service" id="router" />
|
||||||
<argument type="service" id="annotation_reader" />
|
<argument type="service" id="annotation_reader" />
|
||||||
</service>
|
</service>
|
||||||
|
<service id="nelmio_api_doc.form.extension.description_field_type_extension" class="%nelmio_api_doc.form.extension.description_field_type_extension.class%">
|
||||||
|
<tag name="form.type_extension" alias="field" />
|
||||||
|
</service>
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
</container>
|
</container>
|
||||||
|
@ -69,6 +69,7 @@
|
|||||||
<th>Parameter</th>
|
<th>Parameter</th>
|
||||||
<th>Type</th>
|
<th>Type</th>
|
||||||
<th>Required?</th>
|
<th>Required?</th>
|
||||||
|
<th>Description</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@ -77,6 +78,7 @@
|
|||||||
<td>{{ name }}</td>
|
<td>{{ name }}</td>
|
||||||
<td>{{ infos.dataType }}</td>
|
<td>{{ infos.dataType }}</td>
|
||||||
<td>{{ infos.required ? 'true' : 'false' }}</td>
|
<td>{{ infos.required ? 'true' : 'false' }}</td>
|
||||||
|
<td>{{ infos.description }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user