Skip to content

Commit

Permalink
[Form] Added an alternative signature Form::add($name, $type, $options)
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Dec 18, 2012
1 parent d9f7844 commit fb71964
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 71 deletions.
9 changes: 8 additions & 1 deletion UPGRADE-2.2.md
Expand Up @@ -37,7 +37,14 @@

### Form

* The PasswordType is now not trimmed by default.
* The PasswordType is now not trimmed by default.

#### Deprecations

* The methods `getParent()`, `setParent()` and `hasParent()` in
`FormBuilderInterface` were deprecated and will be removed in Symfony 2.3.
You should not rely on these methods in your form type because the parent
of a form can change after building it.

### Routing

Expand Down
Expand Up @@ -11,7 +11,6 @@
namespace Symfony\Bridge\Propel1\Form\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

Expand All @@ -24,13 +23,11 @@ class TranslationFormListener implements EventSubscriberInterface
{
private $columns;
private $dataClass;
private $formFactory;

public function __construct(FormFactoryInterface $formFactory, $columns, $dataClass)
public function __construct($columns, $dataClass)
{
$this->columns = $columns;
$this->dataClass = $dataClass;
$this->formFactory = $formFactory;
}

public static function getSubscribedEvents()
Expand Down Expand Up @@ -78,7 +75,7 @@ public function preSetData(FormEvent $event)

$options = array_merge($options, $customOptions);

$form->add($this->formFactory->createNamed($column, $type, null, $options));
$form->add($column, $type, $options);
}
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Bridge/Propel1/Form/Type/TranslationType.php
Expand Up @@ -28,8 +28,9 @@ class TranslationType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$listener = new TranslationFormListener($builder->getFormFactory(), $options['columns'], $options['data_class']);
$builder->addEventSubscriber($listener);
$builder->addEventSubscriber(
new TranslationFormListener($options['columns'], $options['data_class'])
);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
-----

* TrimListener now removes unicode whitespaces
* deprecated getParent(), setParent() and hasParent() in FormBuilderInterface
* FormInterface::add() now accepts a FormInterface instance OR a field's name, type and options

2.1.0
-----
Expand Down
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -24,11 +23,6 @@
*/
class ResizeFormListener implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface
*/
protected $factory;

/**
* @var string
*/
Expand All @@ -51,9 +45,8 @@ class ResizeFormListener implements EventSubscriberInterface
*/
protected $allowDelete;

public function __construct(FormFactoryInterface $factory, $type, array $options = array(), $allowAdd = false, $allowDelete = false)
public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false)
{
$this->factory = $factory;
$this->type = $type;
$this->allowAdd = $allowAdd;
$this->allowDelete = $allowDelete;
Expand Down Expand Up @@ -90,9 +83,9 @@ public function preSetData(FormEvent $event)

// Then add all rows again in the correct order
foreach ($data as $name => $value) {
$form->add($this->factory->createNamed($name, $this->type, null, array_replace(array(
$form->add((string) $name, $this->type, array_replace(array(
'property_path' => '['.$name.']',
), $this->options)));
), $this->options));
}
}

Expand Down Expand Up @@ -122,9 +115,9 @@ public function preBind(FormEvent $event)
if ($this->allowAdd) {
foreach ($data as $name => $value) {
if (!$form->has($name)) {
$form->add($this->factory->createNamed($name, $this->type, null, array_replace(array(
$form->add((string) $name, $this->type, array_replace(array(
'property_path' => '['.$name.']',
), $this->options)));
), $this->options));
}
}
}
Expand Down
Expand Up @@ -34,7 +34,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

$resizeListener = new ResizeFormListener(
$builder->getFormFactory(),
$options['type'],
$options['options'],
$options['allow_add'],
Expand Down
19 changes: 18 additions & 1 deletion src/Symfony/Component/Form/Form.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\AlreadyBoundException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Util\FormUtil;
Expand Down Expand Up @@ -859,7 +860,7 @@ public function hasChildren()
/**
* {@inheritdoc}
*/
public function add(FormInterface $child)
public function add($child, $type = null, array $options = array())
{
if ($this->bound) {
throw new AlreadyBoundException('You cannot add children to a bound form');
Expand Down Expand Up @@ -888,6 +889,22 @@ public function add(FormInterface $child)
$viewData = $this->getViewData();
}

if (!$child instanceof FormInterface) {
if (!is_string($child)) {
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface');
}

if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
}

if (null === $type) {
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
} else {
$child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
}
}

$this->children[$child->getName()] = $child;

$child->setParent($this);
Expand Down
21 changes: 3 additions & 18 deletions src/Symfony/Component/Form/FormBuilder.php
Expand Up @@ -22,13 +22,6 @@
*/
class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormBuilderInterface
{
/**
* The form factory.
*
* @var FormFactoryInterface
*/
private $factory;

/**
* The children of the form builder.
*
Expand Down Expand Up @@ -63,15 +56,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
{
parent::__construct($name, $dataClass, $dispatcher, $options);

$this->factory = $factory;
}

/**
* {@inheritdoc}
*/
public function getFormFactory()
{
return $this->factory;
$this->setFormFactory($factory);
}

/**
Expand Down Expand Up @@ -125,10 +110,10 @@ public function create($name, $type = null, array $options = array())
}

if (null !== $type) {
return $this->factory->createNamedBuilder($name, $type, null, $options, $this);
return $this->getFormFactory()->createNamedBuilder($name, $type, null, $options, $this);
}

return $this->factory->createBuilderForProperty($this->getDataClass(), $name, null, $options, $this);
return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $name, null, $options, $this);
}

/**
Expand Down
23 changes: 16 additions & 7 deletions src/Symfony/Component/Form/FormBuilderInterface.php
Expand Up @@ -52,6 +52,7 @@ public function create($name, $type = null, array $options = array());
* @throws Exception\FormException if the given child does not exist
*/
public function get($name);

/**
* Removes the field with the given name.
*
Expand All @@ -77,13 +78,6 @@ public function has($name);
*/
public function all();

/**
* Returns the associated form factory.
*
* @return FormFactoryInterface The factory
*/
public function getFormFactory();

/**
* Creates the form.
*
Expand All @@ -97,20 +91,35 @@ public function getForm();
* @param FormBuilderInterface $parent The parent builder
*
* @return FormBuilderInterface The builder object.
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
* should not rely on the parent of a builder, because it is
* likely that the parent is only set after turning the builder
* into a form.
*/
public function setParent(FormBuilderInterface $parent = null);

/**
* Returns the parent builder.
*
* @return FormBuilderInterface The parent builder
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
* should not rely on the parent of a builder, because it is
* likely that the parent is only set after turning the builder
* into a form.
*/
public function getParent();

/**
* Returns whether the builder has a parent.
*
* @return Boolean
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3. You
* should not rely on the parent of a builder, because it is
* likely that the parent is only set after turning the builder
* into a form.
*/
public function hasParent();
}
27 changes: 27 additions & 0 deletions src/Symfony/Component/Form/FormConfigBuilder.php
Expand Up @@ -131,6 +131,11 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*/
private $dataLocked;

/**
* @var FormFactoryInterface
*/
private $formFactory;

/**
* @var array
*/
Expand Down Expand Up @@ -611,6 +616,14 @@ public function getDataLocked()
return $this->dataLocked;
}

/**
* {@inheritdoc}
*/
public function getFormFactory()
{
return $this->formFactory;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -849,6 +862,20 @@ public function setDataLocked($locked)
return $this;
}

/**
* {@inheritdoc}
*/
public function setFormFactory(FormFactoryInterface $formFactory)
{
if ($this->locked) {
throw new FormException('The config builder cannot be modified anymore.');
}

$this->formFactory = $formFactory;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/FormConfigBuilderInterface.php
Expand Up @@ -241,6 +241,13 @@ public function setData($data);
*/
public function setDataLocked($locked);

/**
* Sets the form factory used for creating new forms.
*
* @param FormFactoryInterface $formFactory The form factory.
*/
public function setFormFactory(FormFactoryInterface $formFactory);

/**
* Builds and returns the form configuration.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/FormConfigInterface.php
Expand Up @@ -192,6 +192,13 @@ public function getDataClass();
*/
public function getDataLocked();

/**
* Returns the form factory used for creating new forms.
*
* @return FormFactoryInterface The form factory.
*/
public function getFormFactory();

/**
* Returns all options passed during the construction of the form.
*
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/Form/FormInterface.php
Expand Up @@ -41,14 +41,17 @@ public function getParent();
/**
* Adds a child to the form.
*
* @param FormInterface $child The FormInterface to add as a child
* @param FormInterface|string $child The FormInterface instance or the name of the child.
* @param string|null $type The child's type, if a name was passed.
* @param array $options The child's options, if a name was passed.
*
* @return FormInterface The form instance
*
* @throws Exception\AlreadyBoundException If the form has already been bound.
* @throws Exception\FormException When trying to add a child to a non-compound form.
* @throws Exception\AlreadyBoundException If the form has already been bound.
* @throws Exception\FormException When trying to add a child to a non-compound form.
* @throws Exception\UnexpectedTypeException If $child or $type has an unexpected type.
*/
public function add(FormInterface $child);
public function add($child, $type = null, array $options = array());

/**
* Returns the child with the given name.
Expand Down

0 comments on commit fb71964

Please sign in to comment.