Get form fields from a Symfony 2 AbstractType

advertisements

We build a form in an implementation of the AbstractType. Now we want to retrieve the fields that we defined in the builderForm. I couldn't find any documentation on how to do that though I expect that the part of Symfony that is responsible for outputting the form has to do this as well.

class BlaType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder
                ->add('bla', 'checkbox', array('label' => 'bla', 'required' => true))
                ->add('submit', 'submit', array(
                    'label' => 'bla',
                    'attr' => array('class' => 'btn btn-primary')))
        ;
    }
}


All right, as stated in my comment, I will show you one way to produce an array with name => type (I guess there could be more than one way, but for now this would do the trick).

I created a simple form with more fields as follows:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'text')
            ->add('description', 'textarea')
            ->add('isActive', 'checkbox')
            ->add('published', 'datetime')
            ->add('save', SubmitType::class)
        ;
    }
}

Then in your controller it's important to get a hold of the Form instance once you create the form, like this $builder = $this->createForm(new ArticleType());

From there, you can access your child elements:

public function indexAction(Request $request)
{
    $builder = $this->createForm(new ArticleType());
    // $builder -> Symfony\Component\Form\Form
    // It's important to access the children array before the form is being normalized.
    // Otherwise you will gen an error as follows: FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance
    $fields = array();
    foreach($builder->all() as $name => $child) {
        // $child                           -> Symfony\Component\Form\Form
        // $child->getConfig()              -> Symfony\Component\Form\FormBuilder
        // $child->getConfig()->getType()   -> Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy
        $fields[ $name ] = $child->getConfig()->getType()->getName();
    }

    var_dump($fields);
}

Dumping the array we just created, you should get an output like this one:

array (size=5)
  'title' => string 'text' (length=4)
  'description' => string 'textarea' (length=8)
  'isActive' => string 'checkbox' (length=8)
  'published' => string 'datetime' (length=8)
  'save' => string 'submit' (length=6)

That's from me, hope this helps.