I am new with Symfony2 and FOSUserBundle. I am using Symfony2 version 2.4.1. I would like to extend FOSUserBundle in a few ways. I want to add fields to the registration form, some fields i would like to add to the user table which contains username and password but i would like to add fields that get added to a different table. (I think i have to rewrite the registerController??)
Currently i have inherited FOSUserBundle into my own UserBundle. I have tried to simply add fields to the current registration form by following the documentation but i keep returning this error.
Could not load type "acme_user_registration"
// src/Fixie/UserBundle/Entity/User.php
<?php namespace Fixie\UserBundle\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\Length( * min=3, * max="255", * minMessage="The name is too short.", * maxMessage="The name is too long.", * groups={"Registration", "Profile"} * ) */ protected $name; public function __construct() { parent::__construct(); // your own logic } } I then add the name field to the form builder
// src/Fixie/UserBundle/Form/Type/RegistrationFormType.php
<?php namespace Fixie\UserBundle\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('name'); } public function getName() { return 'acme_user_registration'; } } I then go on to declare the custom form type:
// src/Fixie/UserBundle/Resources/config/services.yml
services: acme_user.registration.form.type: class: Fixie\UserBundle\Form\Type\RegistrationFormType arguments: [%fos_user.model.user.class%] tags: - { name: form.type, alias: acme_user_registration } Then updated config:
fos_user: db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel' firewall_name: main user_class: Fixie\UserBundle\Entity\User registration: form: type: acme_user_registration //AppKernel.php
<?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new Symfony\Bundle\SecurityBundle\SecurityBundle(), new Symfony\Bundle\TwigBundle\TwigBundle(), new Symfony\Bundle\MonologBundle\MonologBundle(), new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), new Symfony\Bundle\AsseticBundle\AsseticBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new Fixie\WelcomeBundle\WelcomeBundle(), new Fixie\UserBundle\UserBundle(), new FOS\UserBundle\FOSUserBundle(), ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); } return $bundles; } public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); } } // Fixie/UserBundle/UserBundle.php
<?php namespace Fixie\UserBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class UserBundle extends Bundle { public function getParent() { return 'FOSUserBundle'; } } My main questions are:
Can anyone see an error with my current code?
In relation to extending the form to have multiple fields being stored in multiple tables. What are the step i have to take? Do i rewrite the logic of the controller and the form???
Any help would be very much appreciated.
app/console container:debug. Can you see in output your form serviceacme_user.registration.form.type?services.ymlfile in your DI? The entity/model won't be a problem as they're not loaded at the point the form is requested and the bundle does not need to have 'FOSUserBundle' as a parent to be able to rewrite the form, this is only really necessary for things like controllers, templates and routing.