FOSUserBundle覆盖注册表

问题描述 投票:3回答:1

我想覆盖FOSUserBundle的注册表单。

我已经关注此页:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.3.x/Resources/doc/overriding_forms.md

我希望表单包含这六个输入:

  1. 招呼
  2. 名字
  3. 电子邮件
  4. 密码
  5. 确认密码

我的文件夹结构:

enter link description here

我的用户类:

    <?php

namespace Bundles\FrontendBundle\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\MinLength(limit="3", message="The name is too short.", groups={"Registration", "Profile"})
     * @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
     */
    //protected $name;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

}

我的service.yml数据:

parameters:
services:
    frontendbundle.registration.form.type:
        class: Bundles\FrontendBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: frontend_user_registration  }
    frontendbundle.form.handler.registration:
        class: Bundles\FrontendBundle\Form\Handler\RegistrationFormHandler
        arguments: ["@fos_user.registration.form", "@request", "@fos_user.user_manager", "@fos_user.mailer", "@fos_user.util.token_generator"]
        scope: request
        public: false

我的config.yml数据:

fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Bundles\FrontendBundle\Entity\User
registration:
    form:
        type: frontend_user_registration
        handler: frontendbundle.form.handler.registration

我的RegistrationFormType类:

    <?php

namespace Bundles\FrontendBundle\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('firstname')
            ->add('lastname')
            ->add('salutation')
            ->add('email', 'email', array('label' => 'form.email', '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 setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => "Bundles\FrontendBundle\Entity\User"
        ));
    }

    public function getName()
    {
        return 'frontend_user_registration';
    }
}

如何使用六个输入在此处为我的计划定义字段?

希望可以有人帮帮我。

symfony fosuserbundle
1个回答
0
投票

只需在RegistrationFormType中添加字段,密码字段就会自动生成确认输入。

<?php

namespace Bundles\FrontendBundle\Form\Type;

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

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    $builder->add('firstname')
            ->add('lastname')
            ->add('salutation')
            ->add('email', 'email', array('label' => 'form.email', '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 setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => "Bundles\FrontendBundle\Entity\User"
));
}

public function getName()
{
    return 'frontend_user_registration';
}
© www.soinside.com 2019 - 2024. All rights reserved.