2

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:

  1. Can anyone see an error with my current code?

  2. 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.

5
  • 1
    Do in console app/console container:debug. Can you see in output your form service acme_user.registration.form.type ? Commented Feb 10, 2014 at 21:02
  • No I cant see it. Thanks again for the help Commented Feb 11, 2014 at 21:58
  • Can you post your AppKernel.php ? Commented Feb 12, 2014 at 8:20
  • added it there @denys281 Commented Feb 13, 2014 at 12:28
  • Are you loading the services.yml file 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. Commented Feb 16, 2014 at 0:28

2 Answers 2

1

Ok.

In appKerenl.php you have

 new Fixie\UserBundle\UserBundle(), 

But in service.yml

class: Acme\UserBundle\Form\Type\RegistrationFormType 

Maybe it should be:

class: Fixie\UserBundle\Form\Type\RegistrationFormType 

If in result of command app/console container:debug you can't see acme_user.registration.form.type it means that your service don't register and fos_user can't see you service, and can't find your form type.

My UserBundle store in Webmil/Joint/UserBundle. In UserBundle folder I have file WebmilJointUserBundle.php

My file:

<?php namespace Webmil\Joint\UserBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class WebmilJointUserBundle extends Bundle { public function getParent() { return 'FOSUserBundle'; } } 

So I think that your problem in this file. Can you compare file and your file, and maybe you will find error.

Sign up to request clarification or add additional context in comments.

6 Comments

I changed that to Fixie\UserBundle\Form\Type\RegistrationFormType but i still cant see acme_user.registration.form.type when i run app/console container:debug. I have amended the above code to reflect this.
So if I put the services part in my app/config.yml i can see it when i go app/console container:debug so it is only not registering the service when it is in the bundle.
@bowseph First I think we need, that your service load from your UserBundle.I think I know where your problem and why even from app/config your service doesn't work. Can you post contents of file Fixie/UserBundle/FixieUserBundle.php ?
I have added the equivalent file from my project and cannot see any difference. I have tried making a new Bundle just to test out if I could register a bundle from there and i was able to. Should i just create a new bundle and move all my files into it and see if it will register then? Thanks for all the help!
Try rename file from UserBundle.php to FixieUserBundle.php. And yes you can create new bundle. I hade same problem, and spent nearly 1 hour to find this problem))
|
0

With me it was that i had already a services.yml in app/config, so the one in my bundle was ignored. In the end I added the registry to this file

# app/config/services.yml services: # [other already existing services] acme_user.registration.form.type: class: Acme\UserBundle\Form\Type\RegistrationFormType tags: - { name: form.type, alias: acme_user_registration } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.