Using Symfony Form 2.3 in Silex

I am trying to create a Symfony form (in Silex) by name. Using the configuration below, I find that I need to call $form = $app['form.factory']->createBuilder('address'); , however, FormRegistry cannot find a form of this type.

 use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormTypeExtensionInterface; class AddressType extends AbstractType implements FormTypeExtensionInterface { public function getName() { return 'address'; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('addressee', 'text'); // .. fields .. $builder->add('country', 'text'); } public function getExtendedType() { return 'form'; } } 

It is then added to the forms registry using the form.type.extensions provider:

 $app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function($extensions) use ($app) { $extensions[] = new AddressType(); return $extensions; })); 

Is there anything else I need to do, or another way to create the form this way?

+4
source share
4 answers

Why not use direct

 $app['form.factory']->createBuilder('Namespace\\Form\\Types\\Form') 
+4
source

First, sorry for my poor English. :)

I think you should extend form.extensions instead of form.type.extensions .

Something like that:

  $app['form.extensions'] = $app->share($app->extend('form.extensions', function($extensions) use ($app) { $extensions[] = new MyTypesExtension(); return $extensions; })); 

Then your MyTypesExtension class should look like this:

 use Symfony\Component\Form\AbstractExtension; class MyTypesExtension extends AbstractExtension { protected function loadTypes() { return array( new AddressType(), //Others custom types... ); } } 

Now you can get your own type as follows:

 $app['form.factory']->createBuilder('address')->getForm(); 

Enjoy it!

+3
source

I see this question is quite old, but:

What you do is create a new type of form that does not extend the existing one, so the correct way to register it is to add it to "form.types". (Remember: a form type extension adds something to existing types, so for the future, all instances will have this new β€œfunction." Here you create your own form type.)

 $app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) { $types[] = new AddressType(); return $types; })); 
+1
source

I think that when you come from Symfony to Silex form.type.extension can be misleading.

From Symfony How to create a form type extension :

  • You want to add a generic function to several types (for example, add β€œhelp” text for each type of field);
  • You want to add a specific function to one type (for example, add the "upload" function to the "file" field type).

Since your code shows that you want to add the FormType that exists in Symfony, but you would use the FormServiceProvider in Silex without defining AbstractType and just use the form.factory service, as shown in this example:

In app.php :

 use Silex\Provider\FormServiceProvider; $app->register(new FormServiceProvider()); 

In your controller / action:

 $form = $app['form.factory']->createBuilder('form', $data) ->add('name') ->add('email') ->add('gender', 'choice', array( 'choices' => array(1 => 'male', 2 => 'female'), 'expanded' => true, )) ->getForm() ; 
0
source

All Articles