Search code examples
symfonysymfony-formssymfony-2.3

Symfony 2.3 submit FormType on eventListener error


I want to setup buttons inside a FormType event listener but when I do that I get that error:

The option auto_initialize does not exist. Known options are: attr, block_name, disabled, label, translation_domain

If I setup out of the event listener it works. Can someone help me?

class ClientType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('enquirer', new ContactType(), array(
                "by_reference" => true,
                "required" => true,
                'validation_groups' => array('required'),
                'details_field' => true,
            ))
            [...]

        ;
        $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
            $client=$event->getData();
            $form=$event->getForm();
            if($client && !$client->getId()){
                $form->add("createBtn", "submit", array("label"=>"Create", "attr"=>array("class"=> "btn btn-primary")));
            } else {
                $form->add("saveBtn", "submit", array("label"=>"Save", "attr"=>array("class"=> "btn btn-primary")));
            }
        });
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Sme\ClientBundle\Entity\Client',
        ));
    }

    public function getName()
    {
        return 'client_form';
    }
}

Stack trace: I haven't setup "auto_initialize" but inside FormFactory it is. Any ideas?

  in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 47   + 
    at FormFactory ->createNamed ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
    in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 819   + 
    at Form ->add ('createBtn', 'submit', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'))) 
    in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\src\Sme\ClientBundle\Form\ClientType.php at line 117

Full stack trace

at OptionsResolver ->validateOptionsExistence (array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 219   + 
at OptionsResolver ->resolve (array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 109   + 
at ResolvedFormType ->createBuilder (object(FormFactory), 'createBtn', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 87   + 
at FormFactory ->createNamedBuilder ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 47   + 
at FormFactory ->createNamed ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false)) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 819   + 
at Form ->add ('createBtn', 'submit', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'))) 
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\src\Sme\ClientBundle\Form\ClientType.php at line 117

Looking at \vendor\symfony\symfony\src\Symfony\Component\Form\Form.php I found that code:

public function add($child, $type = null, array $options = array())
{
[...]
if (!$child instanceof FormInterface) {
            if (!is_string($child) && !is_int($child)) {
                throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
            }

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

            // Never initialize child forms automatically
            $options['auto_initialize'] = false;

            if (null === $type) {
                $child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
            } else {
                $child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
            }
        } elseif ($child->getConfig()->getAutoInitialize()) {
            throw new RuntimeException(sprintf(
                'Automatic initialization is only supported on root forms. You '.
                'should set the "auto_initialize" option to false on the field "%s".',
                $child->getName()
            ));
        }
[...]
}

Now I'am more confused.

Thanks you


Solution

  • Try to upgrade to the last version (currently 2.3.6). It seems that this bug was fixed in 2.3.4.