0
votes

I followed my example from here How to fill the dropdown list dynamically in symfony? (select the cities of the region)

I'm trying to make a nested Form, embed a collection of forms inside a form. i have problem with the buildForm.

In my case, I am trying to select the cities of the region for the Company (Entreprise) entity of the Employer (Employeur) entity.

I have a problem with the addition of entity and I also have a problem in the case of enterprise edition.

When adding the Employer (Employeur) entity: (I can not display the add form)

This example works well with some entities (a simple form, not nested).

But when I tried it with the Employer entity I got this error:

Call to a member function getVille() on null

The Stack Trace:

FatalThrowableError Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function getVille() on null

at src\GE\CandidatBundle\Form\EntrepriseType.php:159 at GE\CandidatBundle\Form\EntrepriseType->GE\CandidatBundle\Form{closure}(object(FormEvent), 'form.pre_set_data', object(EventDispatcher)) at call_user_func(object(Closure), object(FormEvent), 'form.pre_set_data', object(EventDispatcher)) (vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php:212) at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(Closure)), 'form.pre_set_data', object(FormEvent)) (vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php:44) at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('form.pre_set_data', object(FormEvent)) (vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\ImmutableEventDispatcher.php:43) at Symfony\Component\EventDispatcher\ImmutableEventDispatcher->dispatch('form.pre_set_data', object(FormEvent)) (vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:341) at Symfony\Component\Form\Form->setData(null) (vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php:57) at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(Employeur), object(RecursiveIteratorIterator)) (vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:385) at Symfony\Component\Form\Form->setData(object(Employeur)) (vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:489) at Symfony\Component\Form\Form->initialize() (vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php:226) at Symfony\Component\Form\FormBuilder->getForm() (vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php:30) at Symfony\Component\Form\FormFactory->create('GE\CandidatBundle\Form\EmployeurType', object(Employeur), array()) (vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:331) at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('GE\CandidatBundle\Form\EmployeurType', object(Employeur)) (src\GE\CandidatBundle\Controller\EmployeurController.php:44) at GE\CandidatBundle\Controller\EmployeurController->newAction(object(Request)) at call_user_func_array(array(object(EmployeurController), 'newAction'), array(object(Request)))

Employeur entity:

class Employeur extends Utilisateur
{
    /**
     * @ORM\OneToOne(targetEntity="GE\CandidatBundle\Entity\Entreprise", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    protected $entreprise;
}

EntrepriseType:

    $builder
            ->add('raisonSocial')
            ->add('region', EntityType::class, [
                'label' => 'Region *',
                'label_attr' => [
                    "class" => "smaller lighter blue",
                    "style" => "font-size: 21px;",
                ],
                'placeholder' => 'Sélectionnez une region',
                'class' => 'GECandidatBundle:Region',
                'choice_label' => 'nom',
                'multiple' => false,
                'query_builder' => function (RegionRepository $repository) {
                    return $repository->getListeRegion();
                }
            ])
            ->add('ville');
$formModifier = function (FormInterface $form, Region $region = null) {
            $villes = null === $region ? array() : $region->getVilles();
            $form->add('ville', EntityType::class, array(
                'label' => 'Ville *',
                'label_attr' => [
                    "class" => "smaller lighter blue",
                    "style" => "font-size: 21px;",
                ],
                'placeholder' => 'Sélectionnez une ville',
                'class' => 'GECandidatBundle:Ville',
                'choice_label' => 'nom',
                'choices' => $villes,
                'multiple' => false,
                'expanded' => false,
            ));
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                $data = $event->getData();
                $formModifier($event->getForm(), $data->getVille());
            }
        );

        $builder->get('region')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                $region = $event->getForm()->getData();
                $formModifier($event->getForm()->getParent(), $region);
            }
        );

My problem is on this line:

$formModifier($event->getForm(), $data->getVille());

why my example worked well before and now no longer works?

When modifying the Company (Entreprise) entity: (I can not display the edit form)

Type error: Argument 2 passed to GE\CandidatBundle\Form\EntrepriseType::GE\CandidatBundle\Form{closure}() must be an instance of GE\CandidatBundle\Entity\Region or null, instance of Proxies__CG__\GE\CandidatBundle\Entity\Ville given, called in C:\xampp\htdocs\GestionEmploi\src\GE\CandidatBundle\Form\EntrepriseType.php on line 159

I have a problem with this line:

$formModifier = function (FormInterface $form, Region $region = null)

Maybe my problem is in the EmployeurType:

$builder
->add('nom')
->add('prenom')
...
->add('entreprise', EntrepriseType::class);
1
I have the same problem.. yesterday all works right and today the error is showing... I dont understand why... how did you fix it?EsopMx

1 Answers

0
votes

On PRE_DATA-SET Event

$event->getData() // can be null

so code to check if $data != null before you call $data->getVille()

$data = $event->getData(); 
$ville = null; 
if ($data != null) 
    $ville = $event->getData()->getVille();
$formModifier($event->getForm(), $ville);