Reputation: 3135
In a Symfony project, I have a Form EventSubscriber acting on several forms.
It aims to disable each field which is already filled.
In the Subscriber when I use:
$childOptions = $child->getConfig()->getOptions();
I receive all resolved options for a child, I want to get only those passed during the form building. (Because form some FormTypes (i.o. DocumentType) it is not possible to reinject all resolved options, some of them causes troubles).
A FormType example :
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('entity',EntityType::class,array(
'class' => 'AppBundle:User',
'choice_label' => 'username',
))
->addEventSubscriber($this->changesSubscriber); // See next class
}
}
The Subscriber :
class ChangesSubscriber implements EventSubscriberInterface
{
// Disables filled inputs
public function postSetData(FormEvent $event)
{
$form = $event->getForm();
foreach($form->all() as $child)
{
$childName = $child->getName();
$childType = $child->getConfig()->getType()->getName();
// Here I receive all resolved options
// But I only want the options passed above during 'buildForm' ('class','choice_label') :
$childOptions = $child->getConfig()->getOptions();
if(!$child->isEmpty()){
$form->add($childName,$childType,array_merge($childOptions,array('disabled'=>true)));
}
}
}
}
This is one example of many use cases, another example could be : Alsatian\FormBundle ExtensibleSubscriber
-> A formsuscriber to make AJAX submitted choices acceptacles for Choice/Entity/Document Types. At this time, as you can see, I choosed to only take a couple of resolved options, but I'm not satisfied with this solution.
Upvotes: 4
Views: 509
Reputation: 7975
Sounds like you need to change your approach.
Maybe make a custom form type, and some of the options to it should be the options to create the original type, similar to how CollectionType
works.
Maybe it looks a bit like this:
->add('entity', AjaxType::class,array(
'ajax_type' => EntityType:class,
'ajax_options' => [
'class' => 'AppBundle:User',
'choice_label' => 'username',
]
))
That type can add the event that listens for the data and decides what to do.
Upvotes: 1