Skip to content
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

$event->getData() returns null for an embedded form #6128

Closed
waiting-for-dev opened this issue Nov 27, 2012 · 36 comments
Closed

$event->getData() returns null for an embedded form #6128

waiting-for-dev opened this issue Nov 27, 2012 · 36 comments

Comments

@waiting-for-dev
Copy link

Related with #5694

Using $event->getData() within an event for an embedded form return null.

public function buildForm(FormBuilderInterface $builder, array $options)
{
     $builder->addEventListener(FormEvents::POST_SET_DATA, function ($event) {
         die(var_dump($event->getData())); //Return NULL for embedded forms, and the entity for non-embedded forms
     });
}

I have tested it for a non-persisted entity with properties assigned in the controller. But it should be the same for persisted entities, I suppose.

@webmozart
Copy link
Contributor

Hi, thank you for reporting this issue! Could you please publish a fork of symfony-standard that reproduces your problem? In general, this functionality is known to work.

@waiting-for-dev
Copy link
Author

Ok, I will prepare it when I have a bit of time...

Thanks!

@webmozart
Copy link
Contributor

Any news?

@waiting-for-dev
Copy link
Author

Wow, I didn't remember this, to be honest. I close the issue because I find too difficult to get the time... I'll reopen if I have the time to review this.

@webmozart
Copy link
Contributor

GitHub never forgets ;)

@taylorludwig
Copy link

for anyone else that runs across this. I ran into the same issue in symfony 2.1 but performed the same test in 2.3 and it was fixed.

@ghost
Copy link

ghost commented Oct 8, 2013

so, what should we do if we are working with symfony2.1? is there any solution for that?

@stof
Copy link
Member

stof commented Oct 8, 2013

@p-shad your solution is updating to a maintained version of Symfony. The 2.1 serie reached its end of maintenance several months ago

@sibok
Copy link

sibok commented Feb 20, 2014

Hi, i'm on Symfony 2.3.9 and the issue exposed here is still reproducible so it already exists and i'm suffering from it. Could you please reopen that issue?

class ParentFormType extends AbstractType
{
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder->add( 'files', 'collection', array(
'type' => new FileType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
);
}
}

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

/*Each one of bellow calls returns NULL
print_r( $builder->getData() );
print_r( $builder->getForm()->getData() );
*
/
$builder->addEventListener(\Symfony\Component\Form\FormEvents::POST_SET_DATA,
function( \Symfony\Component\Form\FormEvent $event )
{
$data = $event->getData();
//Returns NULL
print_r( $data);
die();
}
);

        $builder->add( 'file', 'file', array(
                                                'required'      => false,
                                                'file_path'     => 'file',
                                                'label'         => 'Select a file to be uploaded',
                                                'constraints'   => array(
                                                    new File(
                                                        array(
                                                            'maxSize' => '1024k',        
                                                        )
                                                    )
                                                )
            )
        );
    }

    public function setDefaultOptions( \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver )
    {
        return $resolver->setDefaults( array() );
    }

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

@rfink
Copy link

rfink commented Mar 8, 2014

Also having this issue

@cordoval
Copy link
Contributor

cordoval commented Mar 8, 2014

Can some do what @webmozart proposed more than a year ago?

@cordoval
Copy link
Contributor

please @sibok close your issue 10300 as it is related to this and is the same issue #10300
@waiting-for-dev please reopen this issue
I will fork SE and try to reproduce with the information given in 10300 and in comment

@stof
Copy link
Member

stof commented Mar 13, 2014

@cordoval this issue has been closed 11 months ago

@stof
Copy link
Member

stof commented Mar 13, 2014

and I don't see the point of closing an opened issue and reopening a closed one. Why not keeping them in their current state ?

@cordoval
Copy link
Contributor

trying to reproduce here https://github.com/cordoval/symfony-standard/pull/13/files, i get of course null but it is more because of other reasons and lack of input as to the templates. I found very bad errors on your code @sibok

@sibok
Copy link

sibok commented Mar 13, 2014

Could you please point me the very bad errors? thx

@cordoval
Copy link
Contributor

file_path is non existant is one, but check the diff above i provided and compare to your code.
It would be great if you do reproduce the problems in a SE like i did but add more details on the views. That would definitely help @webmozart or others to reproduce and tackle the issue.

@sibok
Copy link

sibok commented Mar 13, 2014

Excuses, file_path was an option attribute which works for me due to a File Form Field extension i already made.

Anyway, the issue still persists and as said for stof, i'm aware of that issue for about one year at least. I'm closing the issue right now, do someone know if it's going to be fixed? Thx

@florinutz
Copy link

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){

$event->getData() is null and it shouldn't be.

@maher88
Copy link

maher88 commented Mar 24, 2014

Hi, I have the same problem.
Did anyone found a solution or simplier way to do this?

@InformaticRevolution
Copy link

Hi,

I also have the same problem.

@ghost
Copy link

ghost commented May 22, 2014

Same problem here

@qferr
Copy link

qferr commented Aug 21, 2014

Not resolved ? Because I have the same problem... .

@Djevil83
Copy link

Djevil83 commented Nov 2, 2014

Hi, @webmozart I have the same problem.
I found that the var_dump in the embedded form with:

    $builder
        ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            var_dump($event->getData());
            exit();
        });

always return null when in the parent form the collection field have the option 'allow_add' => true,
When i change this option to 'allow_add' => false, the $event->getData() return the entity as expected.

Could you please re open this issue ?

@MConrotte
Copy link

Hi,

I also have the same problem.
Do someone know if really it's going to be fixed ?

@nothingaa
Copy link

Having the same problem.

@Djevil83
Copy link

@MConrotte and @nothingaa. The problem is still there if you set the option 'allow_add' => false instead of 'allow_add' => true in the parent collection form field or this change nothing ?

@nicolaskern
Copy link

This is rather annoying to have to disable allow_add as, right now, I have to allow adds.

After having given a better insight, I noticed that the child form is called for each item of your collection, plus called the first time without any argument nor data.

The quick fix I found is to test if getData() is null or not. Works for me with allow_add => true.

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    if (null != $event->getData()) {
    }
}

@vdrouet
Copy link

vdrouet commented Oct 1, 2015

Same problem here, thanks @nicolaskern for the tip !

@alrayyes
Copy link

alrayyes commented Nov 3, 2015

Same issue here. Thanks to @nicolaskern as well for the quick fix.

@nicolaskern
Copy link

You're much welcome ;)

@boltunoreh
Copy link

try this maybe:
$data = $event->getData();
$filiation = null == $data ? null : $data->getFiliation();
$formModifier($event->getForm(), $filiation);

@moniprecup
Copy link

Any news about this? I'm having the same problem.

@skrosoft
Copy link

skrosoft commented Oct 18, 2017

In my case, the problem is that I want to add field, depending of the value of the getData() instance and the EventListener doesnt allow me to add field to the form.

@eudiegoborgs
Copy link

Solution found to use POST_SET_DATA, using it, my function to call twice just to do a check for case getData == null not executed

See this:
FormEvents::POST_SET_DATA, function (FormEvent $event) { if (is_null($event->getData())) { return; } *write your code action here* }

@randywatson1979
Copy link

My situation was that I assumed that a collectionType field which already contains existing objects, can have prototype at the same time (triggered by "allow_add => true"), but in fact that causes the data in PRE_SET_DATA event to be null.
I ended up separating this field into two collectionType. One for loading the existing objects (prototype => false) and the second one for prototype. Hope this helps anyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests