Reputation: 5602
I have the following setup:
Entity Result:
/**
* @ORM\OneToMany(targetEntity="Answer", mappedBy="result", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $answers;
Entity Answer:
/**
* @ORM\ManyToOne(targetEntity="Result", inversedBy="answers", cascade={"persist"})
* @ORM\JoinColumn(name="result_id", referencedColumnName="id")
*/
private $result;
Form ResultType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('answers', 'collection', array(
'type' => new AnswerType(),
))
;
}
Form AnswerType: - this one I want to make dynamic, currently is deprecated:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('value', 'choice', array(
'choices' => Answer::getChoices(),
'expanded' => true,
'required' => true,
))
;
}
What I want to do:
Result form has a collection of AnswerType. I want that each of this AnswerType to be dynamic depending on the current Answer is linked to.
For example if I have a function on Answer::getRandomStatus()
- if it is true - the value
field of AnswerType to be text
, choice
otherwise.
My guess was in buildForm
method of AnswerType
, but $builder->getData() returns null
, and I can't make a distinction.
Upvotes: 2
Views: 2284
Reputation: 2004
I´m also looking for this, but want an already implemented solution. Take a look at the source code for the CollectionType, it makes extensive use of these events.
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
Upvotes: 0
Reputation: 386
this should give you the answers you're looking for:
http://symfony.com/doc/2.0/cookbook/form/dynamic_form_modification.html
to have a better understanding of the above you could have a look over:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormEvents.php
Upvotes: 3