我正在尝试覆盖FOSUserBundle,因为我可以在构建表单时放置一个defaut值,这样我就不会在填写表单时得到不合适的验证消息......
我关注the doc。我已加入Etienne/UserBundle/Form/Type/RegistrationFormType.php
<?php
namespace Etienne\UserBundle\Form\Type;
use Symfony\Component\Form\BaseType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->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' => $this->class,
'intention' => 'registration',
));
}
public function getName()
{
return 'etienne_user_registration';
}
}
Etienne/UserBundle/Resources/config/services.yml
:
services:
etienne_user.registration.form.type:
class: Etienne\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: etienne_user_registration }
和app/config
:
FOS_User
registration:
form:
type: etienne_user_registration
我收到了这个错误消息:
/var/www/projet_etienne_auth/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php at 95
if (!$type) {
throw new FormException(sprintf('Could not load type "%s"', $name));
}
$this->resolveAndAddType($type);
答案 0 :(得分:3)
问题是我的Dependency注入文件夹中没有EtienneUserExtension.php文件(不正确的软件包安装),因此无法加载services.xml
答案 1 :(得分:1)
在这段代码中,您首先从Symfony的BaseType
组件中导入Form
类,然后从RegistrationFormType
为FOS
定义具有相同名称的别名。
use Symfony\Component\Form\BaseType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
只需删除此声明(您不需要它),一切都应该没问题:
use Symfony\Component\Form\BaseType;