-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Added an alternative signature Form::add($name, $type, $options) #6355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ CHANGELOG | |
----- | ||
|
||
* TrimListener now removes unicode whitespaces | ||
* deprecated getParent(), setParent() and hasParent() in FormBuilderInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this change. We need this feature in the form type extensions. |
||
* FormInterface::add() now accepts a FormInterface instance OR a field's name, type and options | ||
|
||
2.1.0 | ||
----- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -24,11 +23,6 @@ | |
*/ | ||
class ResizeFormListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var FormFactoryInterface | ||
*/ | ||
protected $factory; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
|
@@ -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; | ||
|
@@ -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)); | ||
} | ||
} | ||
|
||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The form should probably also accept an integer as name, which makes it more fault-tolerant (instead of throwing an exception). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. casting is necessary because of the check in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes thats what I'd like to see improved... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this condition is the same than when adding a child through the builder: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormBuilder.php#L96 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed |
||
'property_path' => '['.$name.']', | ||
), $this->options))); | ||
), $this->options)); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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'); | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing phpdoc for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
|
||
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) { | ||
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface'); | ||
} | ||
|
||
F438 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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please no! We need know the parent data class in the form subscribers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The form subscribers are not accessing the FormBuilder but the Form
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry. But we need this feature in the form type extensions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hason Please read the full deprecation message: this feature is not reliable (and cannot be made reliable without rewriting the full Form building as children can be build before adding them in their parent)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really sad about this :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tiger-seo they never worked properly as the parent form is generally not available yet when calling
buildForm
on the type. The form is added into its parent after building the Form instance from the builder.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof thank you very much for you time. the reason I need property parent is because I have a sophisticated field type (kinda collection of entities the main idea of which is to link entities received and those stored in DB by using ID of entities), because it is not possible with existing collection type, which uses ordinal position, so the reason is - I need to attach my event handler on the root form.
with Symfony 2.2 I get root-form this way:
full listing https://gist.github.com/tiger-seo/67a6e8e7177b3b6c104d
i have no idea how i can get root-form to attach my event handler :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same problem is described in #8607