Rakhitha
Rakhitha

Reputation: 189

Symfony2 entity type check box set preferred choices

I have the following form filed in the edit form.

->add('district', 'entity', array(
                    'class' => 'AdminBundle:Districts',
                               'query_builder' => function(EntityRepository $repository) {
                return $repository->createQueryBuilder('c')
                                    ->where('c.status =:status')
                                    ->setparameter('status','1');
            },
                    'property' => 'districtName',
                    'preferred_choices' => array($details->getDistrict()),
                    'multiple' => TRUE,
                    'expanded' => TRUE,
                    'required' => true,

                        )
                )

Output of this is checkboxes. I can check more districts here. In the edit mode how to set the preferred choices?

Upvotes: 0

Views: 1010

Answers (1)

Matthew A Thomas
Matthew A Thomas

Reputation: 914

OK, you need to use an EventListener against the form. See documentation for more information.

This will allow you to pre set form data

Example ()

/* Form */

 namespace Company\YourBundle\Form\Type;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Doctrine\ORM\EntityRepository;
 use Company\YourBundle\Form\EventListener\YourEventListener;

 class FormType extends AbstractType {

     public function buildForm(FormBuilderInterface $builder, array $options) {
         $builder->addEventSubscriber(new YourEventListener($builder->getFormFactory()));
     }

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

}

/* Event Listener (You may require to pass more data to this class from your form as I have little information to help you with)*/

 namespace Company\YourBundle\Form\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ActivityListener implements EventSubscriberInterface {

private $form;

public function __construct($form) {
    $this->form = $form;
}

public static function getSubscribedEvents() {
    return array(
        FormEvents::PRE_SET_DATA => 'onPreSetData',
    );
}

public function onPreSetData(FormEvent $e) {
    $data = $e->getData();
    $form = $e->getForm();

    if ($form->has('district')) {
        $form->remove('district');
    }

    $form->add($this->form->createNamed('district', 'entity', null, array(
                        'class' => 'AdminBundle:Districts',
                           'query_builder' => function(EntityRepository $repository) {
            return $repository->createQueryBuilder('c')
                                ->where('c.status =:status')
                                ->setparameter('status','1')},
                                'property' => 'districtName',
                'preferred_choices' => $data['id'] ? /** in edit mode set the preferred **/ ? null,
                'multiple' => TRUE,
                'expanded' => TRUE,
                'required' => true,

                    ));
      }

 }

Upvotes: 1

Related Questions