4
votes

I'm trying since yesterday to add a new field in the registration form, but its doesn't work. [fosuserbundle 1.3 -- symfony 2.6.11]

I got an error message:

Attempted to load class "RegistrationFormType" from namespace "FOS\UserBundle\Form". Did you forget a "use" statement for "FOS\UserBundle\Form\Type\RegistrationFormType"?

/protected function getFosUser_Registration_Form_TypeService() {return $this->services['fos_user.registration.form.type'] = new \FOS\UserBundle\Form\RegistrationFormType('FLY\UserBundle\Entity\User'); } /

app/config

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
fos_user:
    db_driver:              orm
    firewall_name:          main
    user_class: FLY\UserBundle\Entity\User
    use_listener:           true
    #use_flash_notifications: true
    use_username_form_type: true
    model_manager_name:     null  # change it to the name of your entity/document manager if you don't want to use the default one.
    from_email:
        address:        [email protected]
        sender_name:    webmaster
    profile:
        form:
            type:               fos_user_profile
            name:               fos_user_profile_form
            validation_groups:  [Profile, Default]
    change_password:
        form:
            type:               fos_user_change_password
            name:               fos_user_change_password_form
            validation_groups:  [ChangePassword, Default]
    registration:
        confirmation:
            from_email: # Use this node only if you don't want the global email address for the confirmation email
                address:       [email protected]
                sender_name:   Webmaster
            enabled:    true # change to true for required email confirmation
            template:   FOSUserBundle:Registration:email.txt.twig
        form:
            type:               fos_user_registration
            name:               fos_user_registration_form
            validation_groups:  [Registration, Default]
    resetting:
        token_ttl: 86400
        email:
            from_email: # Use this node only if you don't want the global email address for the resetting email
                address:        ...
                sender_name:    ...
            template:   FOSUserBundle:Resetting:email.txt.twig
        form:
            type:               fos_user_resetting
            name:               fos_user_resetting_form
            validation_groups:  [ResetPassword, Default]
    service:
        mailer:                 fos_user.mailer.default
        email_canonicalizer:    fos_user.util.canonicalizer.default
        username_canonicalizer: fos_user.util.canonicalizer.default
        token_generator:        fos_user.util.token_generator.default
        user_manager:           fos_user.user_manager.default
    #group:
        #group_class:    ~ # Required when using groups
        #group_manager:  fos_user.group_manager.default
        #form:
            #type:               fos_user_group
            #name:               fos_user_group_form
            #validation_groups:  [Registration, Default]

user.php

<?php
// src/FLY/UserBundle/Entity/User.php

namespace FLY\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(name="name", 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
    }
}

services.yml

services:
    fos_user.registration.form.type:
        class: FOS\UserBundle\Form\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: fos_user_registration }

RegistrationFormType.php

<?php
/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace FOS\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RegistrationFormType extends AbstractType
{
    private $class;
    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
            ->add('username', null, array('label' => 'form.username', '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 configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }
    // BC for SF < 2.7
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->configureOptions($resolver);
    }
    public function getName()
    {
        return 'fos_user_registration';
    }
}

I followed all the instructions in the documentation but it doesn't work.

Thank you

4

4 Answers

2
votes

You have to define where exactly is your RegistrationType in config.yml i have it defineded in this way

registration:
    form:
        type: ClienteBundle\Form\RegistrationType
0
votes

app/config

fos_user:
    ........
    registration:
        form:
            ......
            type: your_form_type_id

services.yml

services:
    user.your_form_type.form.type:
        class: MyBundle\Form\MyRegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: your_form_type_id }

MyRegistrationFormType.php

namespace MyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\UserBundle\Form\RegistrationFormType as BaseType;

class MyRegistrationFormType extends BaseType
{
    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        parent::__construct($class);
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm(builder,$options);
        $builder
            ->add('addon_field1')
            ->add('addon_field2')
        ;
    }

    public function getName()
    {
        return 'your_form_type_id';
    }
0
votes

I had the same issue for a while, it was just that RegistrationFormType.php was not is the right folder. It was in MyBundle/Form/Type but i worked when placed in MyBundle/Form

0
votes

try to check if you make the form folder in AppBundle/Form or not and check again your config.yml have it defineded or not