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

问题描述 投票:4回答:4

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

我收到一条错误消息:

尝试从命名空间“FOS \ UserBundle \ Form”加载类“RegistrationFormType”。您是否忘记了“FOS \ UserBundle \ Form \ Type \ RegistrationFormType”的“use”语句?

/ 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:        [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';
    }
}

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

谢谢

symfony registration fosuserbundle override
4个回答
2
投票

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

registration:
    form:
        type: ClienteBundle\Form\RegistrationType

0
投票

应用程序/配置

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
投票

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


0
投票

尝试检查你是否在AppBundle / Form中制作表单文件夹并再次检查你的config.yml是否已定义

© www.soinside.com 2019 - 2024. All rights reserved.