Search code examples
phpsymfonysymfony-formsproduction-environmentsymfony-3.2

An error is thrown in PROD mode but NOT in DEV mode: "Notice: Undefined index" in $form->getConfig()->getAttributes()


Running my symfony3 project in the prod enviroment on an Ubuntu Server I get the following error:

"Notice: Undefined index: data_collector/passed_options",

This error does not happen if I use the dev environment.

The error is thrown in my custom FormType:

// src/MyBundle/Form/CustomType/MyCustomType.php:
class MyCustomType extends AbstractType {
      public function buildForm(FormBuilderInterface $builder, array $options){
           $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event){....}
            $form = $event->getForm();
            $inheritedAttr = $form->getConfig()->getAttributes()['data_collector/passed_options']['attr']; //it crashes there
             ....   
      }

}

I edited my app_dev.php file on the production Ubuntu Server (Like it is explained here) so that I could test in production using this command:

php bin/console server:start [the IP of my server]:[a custom port]

But the error still didn't get thrown in the dev environment. So it's not a problem with my development machine.

Could it be that $form->getConfig()->getAttributes() has no index in the prod environment?

Is there some way I can debug errors like this that happen in the prod environment but not in the dev environment?


Solution

  • In the addEventListener the $options that is passed as a parameter in the buildForm function should be passed as it contains the attributes:

    class MyCustomType extends AbstractType {
          public function buildForm(FormBuilderInterface $builder, array $options){
               $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) use ($options) {....}
                $form = $event->getForm();
                $inheritedAttr = $options['attr'];
                 ....   
          }
    
    }