Merge pull request #584 from PedroTroller/feature/form-options

Add form options to ApiDoc input
This commit is contained in:
William Durand 2015-03-13 17:22:24 +01:00
commit d8c05dbf57
4 changed files with 28 additions and 17 deletions

View File

@ -17,11 +17,11 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\DataTypes; use Nelmio\ApiDocBundle\DataTypes;
use Nelmio\ApiDocBundle\Parser\ParserInterface; use Nelmio\ApiDocBundle\Parser\ParserInterface;
use Nelmio\ApiDocBundle\Parser\PostParserInterface; use Nelmio\ApiDocBundle\Parser\PostParserInterface;
use Symfony\Component\Routing\Route; use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Util\DocCommentExtractor; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
class ApiDocExtractor class ApiDocExtractor
{ {
@ -366,6 +366,7 @@ class ApiDocExtractor
$defaults = array( $defaults = array(
'class' => '', 'class' => '',
'groups' => array(), 'groups' => array(),
'options' => array(),
); );
// normalize strings // normalize strings

View File

@ -12,16 +12,16 @@
namespace Nelmio\ApiDocBundle\Parser; namespace Nelmio\ApiDocBundle\Parser;
use Nelmio\ApiDocBundle\DataTypes; use Nelmio\ApiDocBundle\DataTypes;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\InvalidArgumentException; use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\View\ChoiceView; use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Component\Form\Form; use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\ResolvedFormTypeInterface; use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\FormException;
class FormTypeParser implements ParserInterface class FormTypeParser implements ParserInterface
{ {
@ -63,9 +63,10 @@ class FormTypeParser implements ParserInterface
public function supports(array $item) public function supports(array $item)
{ {
$className = $item['class']; $className = $item['class'];
$options = $item['options'];
try { try {
if ($this->createForm($className)) { if ($this->createForm($className, null, $options)) {
return true; return true;
} }
} catch (FormException $e) { } catch (FormException $e) {
@ -82,18 +83,17 @@ class FormTypeParser implements ParserInterface
*/ */
public function parse(array $item) public function parse(array $item)
{ {
$type = $item['class']; $type = $item['class'];
$options = $item['options'];
if ($this->implementsType($type)) { if ($this->implementsType($type)) {
$type = $this->getTypeInstance($type); $type = $this->getTypeInstance($type);
} }
$form = $this->formFactory->create($type); $form = $this->formFactory->create($type, null, $options);
$name = array_key_exists('name', $item) ? $item['name'] : $form->getName(); $name = array_key_exists('name', $item) ? $item['name'] : $form->getName();
$options = null;
if (empty($name)) { if (empty($name)) {
return $this->parseForm($form); return $this->parseForm($form);
} }

View File

@ -272,6 +272,14 @@ input = {
} }
``` ```
You can also add some options to pass to the form. You just have to use the `options` key:
```
input = {
"class" = "your_form_type",
"options" = {"method" => "PUT"},
}
```
#### Used Parsers #### Used Parsers
By default, all registered parsers are used, but sometimes you may want to By default, all registered parsers are used, but sometimes you may want to

View File

@ -34,7 +34,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
{ {
return array( return array(
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', 'options' => array()),
array( array(
'a' => array( 'a' => array(
'dataType' => 'string', 'dataType' => 'string',
@ -75,7 +75,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
) )
), ),
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'options' => array()),
array( array(
'collection_type' => array( 'collection_type' => array(
'dataType' => 'object (CollectionType)', 'dataType' => 'object (CollectionType)',
@ -150,6 +150,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
array( array(
'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType',
'name' => '', 'name' => '',
'options' => array(),
), ),
array( array(
'a' => array( 'a' => array(
@ -214,6 +215,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
array( array(
'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', 'class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType',
'name' => null, 'name' => null,
'options' => array(),
), ),
array( array(
'a' => array( 'a' => array(
@ -275,7 +277,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
), ),
), ),
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\ImprovedTestType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\ImprovedTestType', 'options' => array()),
array( array(
'dt1' => array( 'dt1' => array(
'dataType' => 'datetime', 'dataType' => 'datetime',
@ -386,7 +388,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
), ),
), ),
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CompoundType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\CompoundType', 'options' => array()),
array ( array (
'sub_form' => 'sub_form' =>
array ( array (
@ -475,7 +477,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
), ),
), ),
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType', 'options' => array()),
array( array(
'require_construction_type' => array( 'require_construction_type' => array(
'dataType' => 'object (RequireConstructionType)', 'dataType' => 'object (RequireConstructionType)',
@ -500,7 +502,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
), ),
), ),
array( array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType'), array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType', 'options' => array()),
array( array(
'dependency_type' => array( 'dependency_type' => array(
'dataType' => 'object (DependencyType)', 'dataType' => 'object (DependencyType)',