Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony : Can I return null from Type / Form?

I have an entity form with Symfony :

class MyType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       ...
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'LogicielBundle\Entity\FichierGroup',
            'intention' => $this->getName() . '_token'
        ));
    }

But in POST_SUBMIT event, I want to return null (no entity). I tested this but not working :

    $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
        .... my condition ...
        $event->setData(null);
    });

Can you help me ? Thanks :)

like image 329
Gaylord.P Avatar asked Jul 01 '16 09:07

Gaylord.P


1 Answers

Could you please post you controller code? Do you pass an object reference to createForm, or do you use $form->getData() ? In your case, you should stick to the second.

Try using SUBMIT event instead of POST_SUBMIT. As Symfony doc states it, "It can be used to change data from the normalized representation of the data.".

like image 170
romaricdrigon Avatar answered Nov 16 '22 01:11

romaricdrigon