'FOSUserBundle overriding register form
I would like to override the register form from FOSUserBundle.
I have followed this page: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.3.x/Resources/doc/overriding_forms.md
I want that the form to contain these six inputs:
- Salutation
- first name
- last name
- password
- password confirm
My Folder Structure:
My User Class:
<?php
namespace Bundles\FrontendBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
*/
//protected $name;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
My service.yml Data:
parameters:
services:
frontendbundle.registration.form.type:
class: Bundles\FrontendBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: frontend_user_registration }
frontendbundle.form.handler.registration:
class: Bundles\FrontendBundle\Form\Handler\RegistrationFormHandler
arguments: ["@fos_user.registration.form", "@request", "@fos_user.user_manager", "@fos_user.mailer", "@fos_user.util.token_generator"]
scope: request
public: false
My config.yml Data:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Bundles\FrontendBundle\Entity\User
registration:
form:
type: frontend_user_registration
handler: frontendbundle.form.handler.registration
My RegistrationFormType Class:
<?php
namespace Bundles\FrontendBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
// add your custom field
$builder->add('firstname')
->add('lastname')
->add('salutation')
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
)
)
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => "Bundles\FrontendBundle\Entity\User"
));
}
public function getName()
{
return 'frontend_user_registration';
}
}
How do I need to define the field here for my plan with the six inputs ?
Hope someone can help me.
Solution 1:[1]
Just add field in your RegistrationFormType and the password field auto-generate a confirmation input.
<?php
namespace Bundles\FrontendBundle\Form\Type;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('firstname')
->add('lastname')
->add('salutation')
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
)
)
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => "Bundles\FrontendBundle\Entity\User"
));
}
public function getName()
{
return 'frontend_user_registration';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antoine Subit |