Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Form Manager

everzet edited this page Nov 8, 2012 · 8 revisions

The form workflow

Actually, if you want to use a form in a controller, you must:

  • create an entity and an associated form type
  • use the following in your controller's action(s):
$form = $this->createForm(new MyFormType, $entity);
  • optionnally, bind it to the request

We can see multiple drawbacks here:

  • First, we must create a form type which, most of the time will contains trivial informations (if you use php app/console doctrine:generate:form, for instance)
  • We must not forget the use statement of the form type in the controller

The Form Manager service

We propose to address that by introducing a form manager service.

Of course, you don't have to worry about it. You can just use it through shortcuts provided by the base controller.

Usages

Simple as hell

The simplest usage doesn't even need to create a FormType for your entity. The form manager will look into the provided entity class for getXXX and isXXX methods.

If a corresponding setXXX method is found it will automatically add your property to the generated form!

But, it doesn't stop here, as it will do the same for public properties.

Example:

    class MyEntity
    {
        protected $id;
        protected $name;
        protected $lock;
        public $termsOfService;

        public function getId()
        {
            return $this->id;
        }

        public function getName()
        {
            return $this->name;
        }

        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }

        public function isLock()
        {
            return $this->lock;
        }

        public function setLock($lock)
        {
            $this->lock = $lock;

            return $this;
        }
    }
    $entity = new MyEntity();
    $form   = $this->createObjectForm($entity);

The form will contain the following inputs:

  • name
  • lock
  • terms of service

Furthermore, the form creator uses the Form component type guesser functionnality, which means that input types will be guessed given property type and validation

Let's put a simple convention

Of course, you can still define your own form type for your entity. Just make sure to create it in the following namespace App\Form\{EntityName}Type.

My entity has different form types

First, you have to define which is the purpose of your form type. For instance, is it to edit an entity? Is it to create it?

Then, create your form type in the namespace App\Form\{Purpose}{EntityName}Type. Do not forget to put the purpose of your form type as a prefix.

    namespace App\Form;

    class EditMyEntityType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name')
            ;
        }
    }

If you haven't created the purpose associated form type, the form manager will first fallback on the default form type {Entity}Type before creating it for you.

Then, in your controller, you will be able to use this form type by using:

    $entity = new MyEntity();
    $form   = $this->createObjectForm($entity, 'edit');

Tired of binding your form?

    public function editAction()
    {
        $entity = /** fetch somehow an entity */;
        $form   = $manager->createBoundObjectForm($entity, 'edit');
    }

Cool part? It will bind object only if Request is unsafe ('POST', 'PUT', 'DELETE', 'PATCH') and will return unbound form otherways. Also, notice that you don't need to worry about $request object anymore - FormManager will do it on its own!

Clone this wiki locally