a Sensio Labs Product

The PHP micro-framework
based on the Symfony2 Components

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

FormServiceProvider

The FormServiceProvider provides a service for building forms in your application with the Symfony Form component.

Parameters

  • form.secret: This secret value is used for generating and validating the CSRF token for a specific page. It is very important for you to set this value to a static randomly generated value, to prevent hijacking of your forms. Defaults to md5(__DIR__).

Services

Registering

1
2
3
use Silex\Provider\FormServiceProvider;

$app->register(new FormServiceProvider());

Note

If you don't want to create your own form layout, it's fine: a default one will be used. But you will have to register the translation provider as the default form layout requires it.

If you want to use validation with forms, do not forget to register the Validator provider.

Note

The Symfony Form Component and all its dependencies (optional or not) comes with the "fat" Silex archive but not with the regular one. If you are using Composer, add it as a dependency:

1
composer require symfony/form

If you are going to use the validation extension with forms, you must also add a dependency to the symfony/config and symfony/translation components:

1
composer require symfony/validator symfony/config symfony/translation

The Symfony Security CSRF component is used to protect forms against CSRF attacks (as of Symfony 2.4+):

1
composer require symfony/security-csrf

If you want to use forms in your Twig templates, you can also install the Symfony Twig Bridge. Make sure to install, if you didn't do that already, the Translation component in order for the bridge to work:

1
composer require symfony/twig-bridge symfony/translation

Usage

The FormServiceProvider provides a form.factory service. Here is a usage example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$app->match('/form', function (Request $request) use ($app) {
    // some default data for when the form is displayed the first time
    $data = array(
        'name' => 'Your name',
        'email' => 'Your email',
    );

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

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

        // do something with the data

        // redirect somewhere
        return $app->redirect('...');
    }

    // display the form
    return $app['twig']->render('index.twig', array('form' => $form->createView()));
});

And here is the index.twig form template (requires symfony/twig-bridge):

1
2
3
4
5
<form action="#" method="post">
    {{ form_widget(form) }}

    <input type="submit" name="submit" />
</form>

If you are using the validator provider, you can also add validation to your form by adding constraints on the fields:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use Symfony\Component\Validator\Constraints as Assert;

$app->register(new Silex\Provider\ValidatorServiceProvider());
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'translator.domains' => array(),
));

$form = $app['form.factory']->createBuilder('form')
    ->add('name', 'text', array(
        'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
    ))
    ->add('email', 'text', array(
        'constraints' => new Assert\Email()
    ))
    ->add('gender', 'choice', array(
        'choices' => array(1 => 'male', 2 => 'female'),
        'expanded' => true,
        'constraints' => new Assert\Choice(array(1, 2)),
    ))
    ->getForm();

You can register form types by extending form.types:

1
2
3
4
5
$app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
    $types[] = new YourFormType();

    return $types;
}));

You can register form extensions by extending form.extensions:

1
2
3
4
5
$app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions) use ($app) {
    $extensions[] = new YourTopFormExtension();

    return $extensions;
}));

You can register form type extensions by extending form.type.extensions:

1
2
3
4
5
$app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions) use ($app) {
    $extensions[] = new YourFormTypeExtension();

    return $extensions;
}));

You can register form type guessers by extending form.type.guessers:

1
2
3
4
5
$app['form.type.guessers'] = $app->share($app->extend('form.type.guessers', function ($guessers) use ($app) {
    $guessers[] = new YourFormTypeGuesser();

    return $guessers;
}));

Traits

Silex\Application\FormTrait adds the following shortcuts:

  • form: Creates a FormBuilder instance.
1
$app->form($data);

For more information, consult the Symfony Forms documentation.

This website is powered by Silex and Twig. The Silex logo is © 2010,2011 Sensio Labs