我想在SonataUserBundle中扩展或替换注册类型表单.目前,我有使用“easy-extends”命令扩展的sonata用户包.因此,我创建了一个目录“src / Application / Sonata / UserBundle /”.
我的印象是,如果我在“src / Application / Sonata / UserBundle / Form / Type / RegistrationFormType”中创建了另一个文件“RegistrationFormType.php”,那么这个表单将是加载的,而不是vendor文件夹中的表单.
例如,我可以通过在Application文件夹中创建一个文件和一个具有相同名称的类来轻松覆盖registrationController,以替换vendor文件夹中的文件.此外,我可以做同样的事情来成功替换一些树枝模板.但是,它不适用于表单…
表单类型如下所示:
<?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 Sonata\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RegistrationFormType extends AbstractType
{
private $class;
/**
* @var array
*/
protected $mergeOptions;
/**
* @param string $class The User class name
* @param array $mergeOptions Add options to elements
*/
public function __construct($class, array $mergeOptions = array())
{
$this->class = $class;
$this->mergeOptions = $mergeOptions;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array_merge(array(
'label' => 'form.email',
'translation_domain' => 'SonataUserBundle',
), $this->mergeOptions))
->add('plainPassword', 'repeated', array_merge(array(
'type' => 'password',
'options' => array('translation_domain' => 'SonataUserBundle'),
'first_options' => array_merge(array(
'label' => 'form.password',
), $this->mergeOptions),
'second_options' => array_merge(array(
'label' => 'form.password_confirmation',
), $this->mergeOptions),
'invalid_message' => 'fos_user.password.mismatch',
), $this->mergeOptions))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class,
'intention' => 'registration',
));
}
public function getName()
{
return 'sonata_user_registration';
}
}
基本上,我从“vendor / sonata / user-bundle / form / type / registrationFormType.php”复制并粘贴了文件,但我删除了用户名字段,只是为了查看加载了哪个表单.但是,当我访问/注册时,我仍然得到用户名字段,这意味着加载的表单不是我想要的.
那么,我如何重载或替换表单,以便我可以添加字段“firstName,lastName,dateOfBirth等…”,以便用户可以在注册过程中自己输入它们.
编辑1:添加了配置
sonata_user:
security_acl: false
manager_type: orm # Can be orm for mongodb
table:
user_group: "my_custom_user_group_association_table_name"
impersonating:
route: page_slug
parameters: { path: / }
class: # Entity Classes
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
admin: # Admin Classes
user:
class: Sonata\UserBundle\Admin\Entity\UserAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
group:
class: Sonata\UserBundle\Admin\Entity\GroupAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
profile: # Profile Form (firstname, lastname, etc ...)
form:
type: sonata_user_profile
handler: sonata.user.profile.form.handler.default
name: sonata_user_profile_form
validation_groups: [Profile]
register:
form:
type: osc_user_registration
handler: sonata.user.profile.form.handler.default
name: osc_user_registration_form
validation_groups: [SonataUser]
sonata_admin:
templates:
dashboard: SonataAdminBundle:Core:dashboard.html.twig
最佳答案 你没有提到你使用的是soanta版本,我猜不出你的配置是什么.请发布您的配置和奏鸣曲版本.
同时尝试这些行,您必须将您的表单作为服务公开
<service id="acme_user.registration.form.type" class="Acme\UserBundle\Form\Type\RegistrationFormType">
<tag name="form.type" alias="acme_user_registration" />
<argument>%acme_user.model.user.class%</argument>
</service>
并在奏鸣曲配置下,
registration:
form:
type: acme_user_registration
handler: sonata.user.profile.form.handler.default
name: acme_user_registration_form
validation_groups: [AcmeUser]