Ross
Ross

Reputation: 46987

Using Symfony Form 2.3 in Silex

I'm trying to build a Symfony form (in Silex) by name. Using the configuration below, I believe I should be able to call $form = $app['form.factory']->createBuilder('address');, however the 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';
    }
}

This is then added to the form 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 something else I need to do or a different way of building the form in this way?

Upvotes: 4

Views: 4489

Answers (4)

Andras
Andras

Reputation: 171

I see, this question is quite old but:

What you do is creating a new Form Type not extending an existing one, so the correct way to register it to add it to the 'form.types'. (Remember: form type extension is adding something to the existing types so for the future all instance will have that new 'feature'. Here you are creating a custom form type.)

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

Upvotes: 1

Zlatin Hristov
Zlatin Hristov

Reputation: 200

Why not use direct

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

Upvotes: 4

jmpantoja
jmpantoja

Reputation: 31

First, sorry for my poor english. :)

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

Something like this:

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

                        return $extensions;
                    }));

Then your class MyTypesExtension 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 retrieve your custom type this way:

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

Enjoy it!

Upvotes: 3

dazz
dazz

Reputation: 96

I think when you are coming 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 feature to several types (such as adding a "help" text to every field type);
  • You want to add a specific feature to a single type (such as adding a "download" feature to the "file" field type).

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

In your 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()
;

Upvotes: 0

Related Questions