2

I have a field (EntityType => select in the view) in my FormBuilder that I want it to be in initialized with empty data so I can fill it after that in the view via ajax.
So I have read symfony's documentation about EntityType and I found the choices attribute that receives an array of data, so I gave it an empty one 'choices' => array() and it did the trick.
Now the problem is when I submit the form, symfony don't know anymore the type of the field and give me null.
This is the builder:

$buidler->add('supplier', EntityType::class, array(
                'class' => 'SBC\TiersBundle\Entity\Supplier',
                'attr' => array(
                    'class' => 'uk-select uk-select-supplier'
                ),
                'choices' => array(),
            ))

As you can see the the type of the field is SBC\TiersBundle\Entity\Supplier but after submit symfony gives me null !
What should I do to achieve my goal?

2
  • can you post the code on controller side where you create a new instance of the form Commented Feb 14, 2017 at 14:54
  • 1
    @zizoujab Thnx for your answer I already found the solution it's the second answer below. Commented Feb 15, 2017 at 9:04

2 Answers 2

3

All right, this is the solution:
First, I need to pass the EntityManager to my form, and to do this I have created a service:

services:
    payment.supplier.form:
        class: SBC\PaymentBundle\Form\PaymentSupplierType
        tags:
            - { name: form.type, alias: form_em }
        arguments: ["@doctrine.orm.entity_manager"]

Then call the EntityManager in the __construct function:

 private $em;
 private $supplier;

 function __construct(EntityManager $em)
 {
    $this->em = $em;
 }

Second, I need to add two events to the form:
PRE_SUBMIT (to get supplier's code and create Supplier object using the EntityManager):

$builder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function(FormEvent $event){
                $data = $event->getData();
                $code = $data['supplier'];
                $this->supplier = $this->em->getRepository('TiersBundle:Supplier')->find($code);
            }
        );

And finally, use the POST_SUBMIT event to set the supplier object in the submitted data:

$builder->addEventListener(
            FormEvents::POST_SUBMIT,
            function(FormEvent $event){
                $object = $event->getData();
                $object->setSupplier($this->supplier);
                $event->setData($object);

            }
        );

Thanx to Виталий Бойко who gave me a hint about the form events.
So this is what I did with my knowledge and if you have a better solution please share it with us.

Sign up to request clarification or add additional context in comments.

Comments

0

Symfony by default use security for forms, so if you didn't have choices in form builder, you can't pass custom choices to the form after render only via javascript, because you get not valid form. You need to create eventlistener for the form. Check this link for more informationenter link description here, here you can find how to add choices. P.S. sorry for my English)

1 Comment

Thnx for your answer but my entity should be an instance of SBC\TiersBundle\Entity\Supplier and I don't have access to query_builder inside FormEvent to get the entity from database by it's id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.