在注册表单中添加一个新字段,但它不起作用[fosuserbundle]

时间:2015-08-22 13:45:07

标签: symfony registration fosuserbundle override

我从昨天开始尝试在注册表单中添加一个新字段,但它不起作用。 [fosuserbundle 1.3 - symfony 2.6.11]

我收到错误消息:

尝试从命名空间“FOS \ UserBundle \ Form”加载类“RegistrationFormType”。 您是否忘记了“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');     } /

应用/配置

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:        ++++++++@hotmail.fr
        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:       ++++++++@hotmail.fr
                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';
    }
}

我按照文档中的所有说明操作,但它不起作用。

谢谢

4 个答案:

答案 0 :(得分:2)

您必须在config.yml中定义您的RegistrationType的确切位置 我以这种方式定义

registration:
    form:
        type: ClienteBundle\Form\RegistrationType

答案 1 :(得分:0)

应用程序/配置

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';
    }

services.yml

scala> case class Foo1(i: Int)
defined class Foo1

scala> trait Foo1{def testFoo[T](l1 : List[T] ) : List[T] }
defined trait Foo1

MyRegistrationFormType.php

scala> implicit val foodVal = new Foo1{
 |       def testFoo[Foo1](l:List[Foo1] ) = {
 |         for{
 |           a <- l
 |           if (a.i == 1)
 |         } yield a
 |       }
 |     }
<console>:13: error: value i is not a member of type parameter Foo1

答案 2 :(得分:0)

我有一段时间遇到同样的问题,只是RegistrationFormType.php不是正确的文件夹。它在MyBundle/Form/Type,但我放在MyBundle/Form

时工作

答案 3 :(得分:0)

尝试检查是否在AppBundle / Form中创建了Form文件夹,然后再次检查 您的config.yml是否已定义