The Form Component

The Form component allows you to easily create, process and reuse HTML forms.

The Form component is a tool to help you solve the problem of allowing end-users to interact with the data and modify the data in your application. And though traditionally this has been through HTML forms, the component focuses on processing data to and from your client and application, whether that data be from a normal form post or from an API.

Installation

You can install the component in 2 different ways:

Configuration

小技巧

If you are working with the full-stack Symfony framework, the Form component is already configured for you. In this case, skip to Creating a simple Form.

In Symfony, forms are represented by objects and these objects are built by using a form factory. Building a form factory is simple:

use Symfony\Component\Form\Forms;

$formFactory = Forms::createFormFactory();

This factory can already be used to create basic forms, but it is lacking support for very important features:

  • Request Handling: Support for request handling and file uploads;
  • CSRF Protection: Support for protection against Cross-Site-Request-Forgery (CSRF) attacks;
  • Templating: Integration with a templating layer that allows you to reuse HTML fragments when rendering a form;
  • Translation: Support for translating error messages, field labels and other strings;
  • Validation: Integration with a validation library to generate error messages for submitted data.

The Symfony Form component relies on other libraries to solve these problems. Most of the time you will use Twig and the Symfony HttpFoundation, Translation and Validator components, but you can replace any of these with a different library of your choice.

The following sections explain how to plug these libraries into the form factory.

小技巧

For a working example, see https://github.com/bschussek/standalone-forms

Request Handling

2.3 新版功能: The handleRequest() method was introduced in Symfony 2.3.

To process form data, you’ll need to call the handleRequest() method:

$form->handleRequest();

Behind the scenes, this uses a NativeRequestHandler object to read data off of the correct PHP superglobals (i.e. $_POST or $_GET) based on the HTTP method configured on the form (POST is default).

参见

If you need more control over exactly when your form is submitted or which data is passed to it, you can use the submit() for this. Read more about it in the cookbook.

CSRF Protection

Protection against CSRF attacks is built into the Form component, but you need to explicitly enable it or replace it with a custom solution. The following snippet adds CSRF protection to the form factory:

use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
use Symfony\Component\HttpFoundation\Session\Session;

// generate a CSRF secret from somewhere
$csrfSecret = '<generated token>';

// create a Session object from the HttpFoundation component
$session = new Session();

$csrfProvider = new SessionCsrfProvider($session, $csrfSecret);

$formFactory = Forms::createFormFactoryBuilder()
    // ...
    ->addExtension(new CsrfExtension($csrfProvider))
    ->getFormFactory();

To secure your application against CSRF attacks, you need to define a CSRF secret. Generate a random string with at least 32 characters, insert it in the above snippet and make sure that nobody except your web server can access the secret.

Internally, this extension will automatically add a hidden field to every form (called __token by default) whose value is automatically generated and validated when binding the form.

小技巧

If you’re not using the HttpFoundation component, you can use DefaultCsrfProvider instead, which relies on PHP’s native session handling:

use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;

$csrfProvider = new DefaultCsrfProvider($csrfSecret);

Twig Templating

If you’re using the Form component to process HTML forms, you’ll need a way to easily render your form as HTML form fields (complete with field values, errors, and labels). If you use Twig as your template engine, the Form component offers a rich integration.

To use the integration, you’ll need the TwigBridge, which provides integration between Twig and several Symfony components. If you’re using Composer, you could install the latest 2.3 version by adding the following require line to your composer.json file:

{
    "require": {
        "symfony/twig-bridge": "2.3.*"
    }
}

The TwigBridge integration provides you with several Twig Functions that help you render the HTML widget, label and error for each field (as well as a few other things). To configure the integration, you’ll need to bootstrap or access Twig and add the FormExtension:

use Symfony\Component\Form\Forms;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;

// the Twig file that holds all the default markup for rendering forms
// this file comes with TwigBridge
$defaultFormTheme = 'form_div_layout.html.twig';

$vendorDir = realpath(__DIR__.'/../vendor');
// the path to TwigBridge so Twig can locate the
// form_div_layout.html.twig file
$vendorTwigBridgeDir =
    $vendorDir.'/symfony/twig-bridge/Symfony/Bridge/Twig';
// the path to your other templates
$viewsDir = realpath(__DIR__.'/../views');

$twig = new Twig_Environment(new Twig_Loader_Filesystem(array(
    $viewsDir,
    $vendorTwigBridgeDir.'/Resources/views/Form',
)));
$formEngine = new TwigRendererEngine(array($defaultFormTheme));
$formEngine->setEnvironment($twig);
// add the FormExtension to Twig
$twig->addExtension(
    new FormExtension(new TwigRenderer($formEngine, $csrfProvider))
);

// create your form factory as normal
$formFactory = Forms::createFormFactoryBuilder()
    // ...
    ->getFormFactory();

The exact details of your Twig Configuration will vary, but the goal is always to add the FormExtension to Twig, which gives you access to the Twig functions for rendering forms. To do this, you first need to create a TwigRendererEngine, where you define your form themes (i.e. resources/files that define form HTML markup).

For general details on rendering forms, see How to Customize Form Rendering.

注解

If you use the Twig integration, read “Translation” below for details on the needed translation filters.

Translation

If you’re using the Twig integration with one of the default form theme files (e.g. form_div_layout.html.twig), there are 2 Twig filters (trans and transChoice) that are used for translating form labels, errors, option text and other strings.

To add these Twig filters, you can either use the built-in TranslationExtension that integrates with Symfony’s Translation component, or add the 2 Twig filters yourself, via your own Twig extension.

To use the built-in integration, be sure that your project has Symfony’s Translation and Config components installed. If you’re using Composer, you could get the latest 2.3 version of each of these by adding the following to your composer.json file:

{
    "require": {
        "symfony/translation": "2.3.*",
        "symfony/config": "2.3.*"
    }
}

Next, add the TranslationExtension to your Twig_Environment instance:

use Symfony\Component\Form\Forms;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Bridge\Twig\Extension\TranslationExtension;

// create the Translator
$translator = new Translator('en');
// somehow load some translations into it
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource(
    'xlf',
    __DIR__.'/path/to/translations/messages.en.xlf',
    'en'
);

// add the TranslationExtension (gives us trans and transChoice filters)
$twig->addExtension(new TranslationExtension($translator));

$formFactory = Forms::createFormFactoryBuilder()
    // ...
    ->getFormFactory();

Depending on how your translations are being loaded, you can now add string keys, such as field labels, and their translations to your translation files.

For more details on translations, see Translations.

Validation

The Form component comes with tight (but optional) integration with Symfony’s Validator component. If you’re using a different solution for validation, no problem! Simply take the submitted/bound data of your form (which is an array or object) and pass it through your own validation system.

To use the integration with Symfony’s Validator component, first make sure it’s installed in your application. If you’re using Composer and want to install the latest 2.3 version, add this to your composer.json:

{
    "require": {
        "symfony/validator": "2.3.*"
    }
}

If you’re not familiar with Symfony’s Validator component, read more about it: Validation. The Form component comes with a ValidatorExtension class, which automatically applies validation to your data on bind. These errors are then mapped to the correct field and rendered.

Your integration with the Validation component will look something like this:

use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Validator\Validation;

$vendorDir = realpath(__DIR__.'/../vendor');
$vendorFormDir = $vendorDir.'/symfony/form/Symfony/Component/Form';
$vendorValidatorDir =
    $vendorDir.'/symfony/validator/Symfony/Component/Validator';

// create the validator - details will vary
$validator = Validation::createValidator();

// there are built-in translations for the core error messages
$translator->addResource(
    'xlf',
    $vendorFormDir.'/Resources/translations/validators.en.xlf',
    'en',
    'validators'
);
$translator->addResource(
    'xlf',
    $vendorValidatorDir.'/Resources/translations/validators.en.xlf',
    'en',
    'validators'
);

$formFactory = Forms::createFormFactoryBuilder()
    // ...
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory();

To learn more, skip down to the Form Validation section.

Accessing the Form Factory

Your application only needs one form factory, and that one factory object should be used to create any and all form objects in your application. This means that you should create it in some central, bootstrap part of your application and then access it whenever you need to build a form.

注解

In this document, the form factory is always a local variable called $formFactory. The point here is that you will probably need to create this object in some more “global” way so you can access it from anywhere.

Exactly how you gain access to your one form factory is up to you. If you’re using a Service Container, then you should add the form factory to your container and grab it out whenever you need to. If your application uses global or static variables (not usually a good idea), then you can store the object on some static class or do something similar.

Regardless of how you architect your application, just remember that you should only have one form factory and that you’ll need to be able to access it throughout your application.

Creating a simple Form

小技巧

If you’re using the Symfony framework, then the form factory is available automatically as a service called form.factory. Also, the default base controller class has a createFormBuilder() method, which is a shortcut to fetch the form factory and call createBuilder on it.

Creating a form is done via a FormBuilder object, where you build and configure different fields. The form builder is created from the form factory.

  • Standalone Use
    $form = $formFactory->createBuilder()
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();
    
    echo $twig->render('new.html.twig', array(
        'form' => $form->createView(),
    ));
    
  • Framework Use
    // src/Acme/TaskBundle/Controller/DefaultController.php
    namespace Acme\TaskBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    
    class DefaultController extends Controller
    {
        public function newAction(Request $request)
        {
            // createFormBuilder is a shortcut to get the "form factory"
            // and then call "createBuilder()" on it
            $form = $this->createFormBuilder()
                ->add('task', 'text')
                ->add('dueDate', 'date')
                ->getForm();
    
            return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
                'form' => $form->createView(),
            ));
        }
    }
    

As you can see, creating a form is like writing a recipe: you call add for each new field you want to create. The first argument to add is the name of your field, and the second is the field “type”. The Form component comes with a lot of built-in types.

Now that you’ve built your form, learn how to render it and process the form submission.

Setting default Values

If you need your form to load with some default values (or you’re building an “edit” form), simply pass in the default data when creating your form builder:

  • Standalone Use
    $defaults = array(
        'dueDate' => new \DateTime('tomorrow'),
    );
    
    $form = $formFactory->createBuilder('form', $defaults)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();
    
  • Framework Use
    $defaults = array(
        'dueDate' => new \DateTime('tomorrow'),
    );
    
    $form = $this->createFormBuilder($defaults)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();
    

小技巧

In this example, the default data is an array. Later, when you use the data_class option to bind data directly to objects, your default data will be an instance of that object.

Rendering the Form

Now that the form has been created, the next step is to render it. This is done by passing a special form “view” object to your template (notice the $form->createView() in the controller above) and using a set of form helper functions:

<form action="#" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>
../../_images/form-simple.png

That’s it! By printing form_widget(form), each field in the form is rendered, along with a label and error message (if there is one). As easy as this is, it’s not very flexible (yet). Usually, you’ll want to render each form field individually so you can control how the form looks. You’ll learn how to do that in the “Rendering a Form in a Template” section.

Changing a Form’s Method and Action

2.3 新版功能: The ability to configure the form method and action was introduced in Symfony 2.3.

By default, a form is submitted to the same URI that rendered the form with an HTTP POST request. This behavior can be changed using the action and method options (the method option is also used by handleRequest() to determine whether a form has been submitted):

  • Standalone Use
    $formBuilder = $formFactory->createBuilder('form', null, array(
        'action' => '/search',
        'method' => 'GET',
    ));
    
    // ...
    
  • Framework Use
    // ...
    
    public function searchAction()
    {
        $formBuilder = $this->createFormBuilder('form', null, array(
            'action' => '/search',
            'method' => 'GET',
        ));
    
        // ...
    }
    

Handling Form Submissions

To handle form submissions, use the handleRequest() method:

  • Standalone Use
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    $form = $formFactory->createBuilder()
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();
    
    $request = Request::createFromGlobals();
    
    $form->handleRequest($request);
    
    if ($form->isValid()) {
        $data = $form->getData();
    
        // ... perform some action, such as saving the data to the database
    
        $response = new RedirectResponse('/task/success');
        $response->prepare($request);
    
        return $response->send();
    }
    
    // ...
    
  • Framework Use
    // ...
    
    public function newAction(Request $request)
    {
        $form = $this->createFormBuilder()
            ->add('task', 'text')
            ->add('dueDate', 'date')
            ->getForm();
    
        $form->handleRequest($request);
    
        if ($form->isValid()) {
            $data = $form->getData();
    
            // ... perform some action, such as saving the data to the database
    
            return $this->redirect($this->generateUrl('task_success'));
        }
    
        // ...
    }
    

This defines a common form “workflow”, which contains 3 different possibilities:

  1. On the initial GET request (i.e. when the user “surfs” to your page), build your form and render it;

If the request is a POST, process the submitted data (via handleRequest()). Then:

  1. if the form is invalid, re-render the form (which will now contain errors);
  2. if the form is valid, perform some action and redirect.

Luckily, you don’t need to decide whether or not a form has been submitted. Just pass the current request to the handleRequest() method. Then, the Form component will do all the necessary work for you.

Form Validation

The easiest way to add validation to your form is via the constraints option when building each field:

  • Standalone Use
    use Symfony\Component\Validator\Constraints\NotBlank;
    use Symfony\Component\Validator\Constraints\Type;
    
    $form = $formFactory->createBuilder()
        ->add('task', 'text', array(
            'constraints' => new NotBlank(),
        ))
        ->add('dueDate', 'date', array(
            'constraints' => array(
                new NotBlank(),
                new Type('\DateTime'),
            )
        ))
        ->getForm();
    
  • Framework Use
    use Symfony\Component\Validator\Constraints\NotBlank;
    use Symfony\Component\Validator\Constraints\Type;
    
    $form = $this->createFormBuilder()
        ->add('task', 'text', array(
            'constraints' => new NotBlank(),
        ))
        ->add('dueDate', 'date', array(
            'constraints' => array(
                new NotBlank(),
                new Type('\DateTime'),
            )
        ))
        ->getForm();
    

When the form is bound, these validation constraints will be applied automatically and the errors will display next to the fields on error.

注解

For a list of all of the built-in validation constraints, see Validation Constraints Reference.

Accessing Form Errors

You can use the getErrors() method to access the list of errors. Each element is a FormError object:

$form = ...;

// ...

// an array of FormError objects, but only errors attached to this
// form level (e.g. "global errors)
$errors = $form->getErrors();

// an array of FormError objects, but only errors attached to the
// "firstName" field
$errors = $form['firstName']->getErrors();

// a string representation of all errors of the whole form tree
$errors = $form->getErrorsAsString();

注解

If you enable the error_bubbling option on a field, calling getErrors() on the parent form will include errors from that field. However, there is no way to determine which field an error was originally attached to.