Wednesday, September 9, 2015

Add some attributes or set options data to the existing field of an entity

Add some attributes or set options data to the existing field of an entity:

# vim src/My/DemoBundle/Form/Type/ContactType.php

<?php
namespace My\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

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

class ContactType extends AbstractType
{
    private $idJobFieldOptArr;


    public function __construct($optArr)
    {
        $this->idJobFieldOptArr = $optArr['idJobFieldOptArr'];
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstname', 'text', array(
          'required' => FALSE,
        ));
        $builder->add('lastname', 'text', array(
          'required' => FALSE,
        ));
        $builder->add('idjobfield', 'choice', array(
          'required' => FALSE,
          'choices' => $this->idJobFieldOptArr,
          'placeholder' => '--- select ---',
        ));

        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            $entity = $event->getData();
            $form = $event->getForm();

            if ($entity !== NULL && $entity->getId()) {
              $_id = $entity->getId();

              $firstnameOrig = $form->get('firstname')->getConfig()->getOptions();
              $firstnameOrig['attr'] = array('data-test' => 'test ' . $_id);

              $form->add('firstname', 'text', $firstnameOrig);
            }
        });
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\DemoBundle\Entity\Contact',
            #'idJobFieldOptArr' => null,
        ));
    }
}
?>

# vim src/My/DemoBundle/Controller/ContactController.php

<?php
namespace My\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use My\DemoBundle\Entity\User;
use My\DemoBundle\Entity\Contact;

use My\DemoBundle\Form\Type\ContactType;

class ContactController extends Controller
{
    public function addAction(Request $request)
    {
        $idJobFieldOptArr = array(
          1 => 'Sales',
          2 => 'Accounting',
          3 => 'Management',
        );

        $user = self::myFindByEmail($email);

        if (!$user) {
          $user = new User();
        }

        $user->addContact(new Contact());

        $formName = 'contactsform';

        $formDefault = array(
            'email' => 'test@example.com',
            'contacts' => $user->getContacts(),
        );
        $formAttr = array('attr' => array(
          'class' => $formName,
        ));

        $form = $this->get('form.factory')->createNamedBuilder($formName, 'form', $formDefault, $formAttr)
          ->add('email', 'email', array(
            'label' => 'Email',
          ))
          ->add('contacts', 'collection', array(
            'type' => new ContactType(['idJobFieldOptArr' => $idJobFieldOptArr]),
            'allow_add' => TRUE,
            'allow_delete' => TRUE,
            #'options'  => array(
            #  #'idJobFieldOptArr' => $idJobFieldOptArr,
            #),
          ))
          ->add('save', 'submit', array('label' => 'Save'))
          ->getForm();
    }

    private function myFindByEmail($email)
    {
      return $this->getDoctrine()
               ->getRepository('MyDemoBundle:User')
               ->findOneByEmail($email);
    }

}
?>

No comments: