vendor/bundles/FOS/UserBundle/Resources/doc/overriding_forms.md
author ymh <ymh.work@gmail.com>
Sun, 06 Nov 2011 23:44:37 +0100
changeset 27 1df556b2c0f9
parent 3 e54dfe4d0b2b
permissions -rwxr-xr-x
Correct memory problem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
Overriding Default FOSUserBundle Forms
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
======================================
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
## Overriding a Form Type
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
The default forms packaged with the FOSUserBundle provide functionality for 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
registering new user, updating your profile, changing your password and 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
much more. These forms work well with the bundle's default classes and controllers. 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
But, as you start to add more properties to your `User` 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
class or you decide you want to add a few options to the registration form you 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
will find that you need to override the forms in the bundle.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
Suppose that you have created an ORM user class with the following class name, 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
`Acme\UserBundle\Entity\User`. In this class, you have added a `name` property 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
because you would like to save the user's name as well as their username and 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
email address. Now, when a user registers for your site they should enter in their 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
name as well as their username, email and password. Below is an example `$name` 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
property and its validators.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
// src/Acme/UserBundle/Entity/User.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
use FOS\UserBundle\Entity\User as BaseUser;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
use Doctrine\ORM\Mapping as ORM;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
use Symfony\Component\Validator\Constraints as Assert;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
class User extends BaseUser
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
     * @ORM\Id
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
     * @ORM\Column(type="integer")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
     * @ORM\GeneratedValue(strategy="AUTO")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    protected $id;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
     * @ORM\Column(type="string", length="255")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
     *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
     * @Assert\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
     * @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    protected $name;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    // ...
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
By default, the Registration validation group is used when validating a new 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
user registration. Unless you have overriden this value in the configuration, 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
make sure you add the validation group named Registration to your name property.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
If you try and register using the default registration form you will find that 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
your new `name` property is not part of the form. You need to create a custom 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
form type and configure the bundle to use it.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
The first step is to create a new form type in your own bundle. The following 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
class extends the base FOSUserBundle `RegistrationFormType` and then adds the 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
custom `name` field.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
// src/Acme/UserBundle/Form/Type/RegistrationFormType.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
namespace Acme\UserBundle\Form\Type;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
use Symfony\Component\Form\FormBuilder;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
class RegistrationFormType extends BaseType
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    public function buildForm(FormBuilder $builder, array $options)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        parent::buildForm($builder, $options);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        // add your custom field
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        $builder->add('name');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    public function getName()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        return 'acme_user_registration';
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
Now that you have created your custom form type, you must declare it as a service 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
and add a tag to it. The tag must have a `name` value of `form.type` and an `alias` 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
value that is the equal to the string returned from the `getName` method of your 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
form type class. The `alias` that you specify is what you will use in the FOSUserBundle 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
configuration to let the bundle know that you want to use your custom form.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
Below is an example of configuring your form type as a service in XML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
``` xml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
<!-- src/Acme/UserBundle/Resources/config/services.xml -->
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
<?xml version="1.0" encoding="UTF-8" ?>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
<container xmlns="http://symfony.com/schema/dic/services"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    <services>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        <service id="acme_user.registration.form.type" class="Acme\UserBundle\Form\Type\RegistrationFormType">
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            <tag name="form.type" alias="acme_user_registration" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
            <argument>%fos_user.model.user.class%</argument>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        </service>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    </services>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
</container>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
Or if you prefer YAML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
# src/Acme/UserBundle/Resources/config/services.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
services:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    acme_user.registration.form.type:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        class: Acme\UserBundle\Form\Type\RegistrationFormType
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
        arguments: [%fos_user.model.user.class%]
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        tags:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            - { name: form.type, alias: acme_user_registration }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
In the form type service configuration you have specified the `fos_user.model.user.class` 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
container parameter as a constructor argument. Unless you have redefined the 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
constructor in your form type class, you must include this argument as it is a 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
requirement of the FOSUserBundle form type that you extended.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
Finally, you must update the configuration of the FOSUserBundle so that it will 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
use your form type instead of the default one. Below is the configuration for 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
changing the registration form type in YAML.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
# app/config/config.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
fos_user:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    # ...
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    registration:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        form:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
            type: acme_user_registration
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
Note how the `alias` value used in your form type's service configuration tag 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
is used in the bundle configuration to tell the FOSUserBundle to use your custom 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
form type.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
## Overriding Form Handlers
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
There are two ways to override the default functionality provided by the 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
FOSUserBundle form handlers. The easiest way is to  override the `onSuccess` 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
method of the handler. The `onSuccess` method is called after the form has been 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
bound and validated.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
The second way is to override the `process` method. Overriding 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
the `process` method should only be necessary when more advanced functionality 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
is necessary when binding and validating the form.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
Suppose you want to add some functionality that takes place after a successful 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
user registration. First you need to create a new class that extends 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
`FOS\UserBundle\Form\Handler\RegistrationFormHandler` and then override the 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
protected `onSuccess` method.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
// src/Acme/UserBundle/Form/Handler/RegistrationFormHandler.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
namespace Acme\UserBundle\Form\Handler;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
use FOS\UserBundle\Model\UserInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
class RegistrationFormHandler extends BaseHandler
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    protected function onSuccess(UserInterface $user, $confirmation)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        // Note: if you plan on modifying the user then do it before calling the 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        // parent method as the parent method will flush the changes
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        parent::onSuccess($user, $confirmation);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        // otherwise add your functionality here
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
If you do not call the onSuccess method of the parent class then the default 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
logic that the FOSUserBundle handler normally executes upon a successful 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
submission will not be performed.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
You can also choose to override the `process` method of the handler. If you choose 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
to override the `process` method then you will be responsible for binding the form 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
data and validating it, as well as implementing the logic required upon a 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
successful submission.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
// src/Acme/UserBundle/Form/Handler/RegistrationFormHandler.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
namespace Acme\UserBundle\Form\Handler;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
class RegistrationFormHandler extends BaseHandler
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    public function process($confirmation = false)  
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        $user = $this->userManager->createUser();
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        $this->form->setData($user);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        if ('POST' == $this->request->getMethod()) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
            $this->form->bindRequest($this->request);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
            if ($this->form->isValid()) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
              
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
                // do your custom logic here
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
                return true;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
            }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        return false;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
The process method should return true for a successful submission and false 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
otherwise.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
Now that you have created and implemented your custom form handler class, you 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
must configure it as a service in the container. Below is an example of 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
configuring your form handler as a service in XML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
``` xml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
<!-- src/Acme/UserBundle/Resources/config/services.xml -->
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
<?xml version="1.0" encoding="UTF-8" ?>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
<container xmlns="http://symfony.com/schema/dic/services"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
    <services>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        <service id="acme_user.form.handler.registration" class="Acme\UserBundle\Form\Handler\RegistrationFormHandler" scope="request" public="false">
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            <argument type="service" id="fos_user.registration.form" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
            <argument type="service" id="request" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            <argument type="service" id="fos_user.user_manager" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            <argument type="service" id="fos_user.mailer" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        </service>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    </services>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
</container>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
Or if you prefer YAML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
# src/Acme/UserBundle/Resources/config/services.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
services:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
    acme_user.form.handler.registration:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        class: Acme\UserBundle\Form\Handler\RegistrationFormHandler
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        arguments: ["@fos_user.registration.form", "@request", "@fos_user.user_manager", "@fos_user.mailer"]
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        scope: request
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        public: false
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
Here you have injected other services as arguments to the constructor of our class 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
because these arguments are required by the base FOSUserBundle form handler class 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
which you extended.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
Now that your new form handler has been configured in the container, all that is 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
left to do is update the FOSUserBundle configuration.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
# app/config/config.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
fos_user:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
    # ...
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
    registration:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        form:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
            handler: acme_user.form.handler.registration
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
Note how the `id` of your configured service is used in the bundle configuration 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
to tell the FOSUserBundle to use your custom form handler.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
At this point, when a user registers on your site your service will be used to 
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
handle the form submission.