【发布时间】:2019-08-23 03:34:17
【问题描述】:
我正在将网站从 symfony 2.6 迁移到 3.4,但我在注册表格和 FOSUserBundle 方面遇到了一些问题。我有这个错误弹出。我检查了他的代码,在他的 src\Pix\UserBundle\Form\RegistrationFormType.php 下看到了另一个文件夹“Type”,其中有一个同名文件:“RegistrationFormType.php”
我尝试使用从那里获取的最新样式修改表单:https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#form
app/config/config.yml:
fos_user:
db_driver: orm
firewall_name: main
user_class: Pix\UserBundle\Entity\User
registration:
form:
type: Pix\UserBundle\Form\RegistrationFormType
validation_groups: [ pix_registration, Default ]
Pix/UserBundle/Resources/config/services.yml下的服务文件
services:
pix_userbundle_registrationformtype:
class: Pix\UserBundle\Form\Type\RegistrationFormType
arguments: ["%fos_user.model.user.class%"]
tags:
- { name: form.type }
Pix/UserBundle/Form/RegistrationFormType.php:
namespace Pix\UserBundle\Form;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RegistrationFormType extends AbstractType
{
public function __construct($class = "User")
{
parent::__construct($class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('username', 'text', array(
'label' => 'Pseudonyme',
))
->add('email', 'text', array(
'label' => 'Adresse mail',
))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'first_options' => array('label' => 'Mot de passe'),
'second_options' => array('label' => 'Confirmation'),
'invalid_message' => 'Mot de passe incorrect',
))
->add('enabled', 'checkbox', array(
'label' => "Activé",
'required' => false
))
->add('firstname', 'text', array(
'label' => 'Prénom',
))
->add('lastname', 'text', array(
'label' => 'Nom',
))
->add('phone', 'text', array(
'label' => 'Téléphone',
))
->add('street', 'text', array(
'label' => "Adresse postale",
'required' => true
))
->add('number', 'text', array(
'label' => "Numéro d'adresse",
'required' => true
))
->add('npa', 'text', array(
'label' => "Code postal",
'required' => true
))
->add('city', 'text', array(
'label' => "Ville",
'required' => true
))
->add('state', 'text', array(
'label' => "Canton",
'required' => true
))
->add('country', 'text', array(
'label' => "Pays",
'required' => true
))
->add('latitude', 'text', array(
'label' => "Latitude",
'required' => true
))
->add('longitude', 'text', array(
'label' => "Longitude",
'required' => true
))
->add('code', 'text', array(
'label' => 'Code de parrainage',
'required' => false
))
->add('pro', 'choice', array(
'label' => "Professionel",
'choices' => array(
true => 'Oui',
false => 'Non',
),
'expanded' => false,
'multiple' => false,
'required' => true
))
->add('iban', 'text', array(
'label' => 'IBAN',
'required' => false
))
->add('distance', 'text', array(
'label' => "Rayon d'intervention",
'required' => false
))
->add('picture', new DocumentType(), array('required' => false))
->add('curriculum', new DocumentType(), array('required' => false))
->add('motivation', new DocumentType(), array('required' => false))
->add('document', new DocumentType(), array('required' => false))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Pix\UserBundle\Entity\User'
));
}
public function getParent()
{
return BaseRegistrationFormType::class;
}
public function getBlockPrefix()
{
return 'pix_userbundle_registrationformtype';
}
}
最后是 Pix/UserBundle/Form/Type/RegistrationFormType.php:
namespace Pix\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType;
use Symfony\Component\Form\AbstractType;
use Pix\UserBundle\Form\DocumentType;
use Pix\UserBundle\Entity\AbilityRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class RegistrationFormType extends AbstractType
{
public function __construct($class = "User")
{
parent::__construct($class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->remove('username');
$builder
->add('firstname', TexType::class, array(
'label' => 'Prénom',
))
->add('lastname', TexType::class, array(
'label' => 'Nom',
))
->add('phone', TexType::class, array(
'label' => 'Téléphone',
))
->add('street', 'hidden')
->add('number', 'hidden')
->add('npa', 'hidden')
->add('city', 'hidden')
->add('state', 'hidden')
->add('country', 'hidden')
->add('latitude', 'hidden')
->add('longitude', 'hidden')
->add('code', TexType::class, array(
'label' => 'Code de parrainage',
'required' => false
))
/*
->add('contact', 'choice', array(
'label' => 'Moyen de contact favorisé',
'choices' => array(
true => 'Par téléphone',
false => 'Par e-mail',
),
'expanded' => true,
'multiple' => false,
'required' => true
))
*/
->add('pro', 'choice', array(
'label' => false,
'choices' => array(
true => 'Oui',
false => 'Non',
),
'expanded' => false,
'multiple' => false,
'required' => true
))
->add('iban', TexType::class, array(
'label' => 'IBAN',
'required' => false
))
->add('distance', TexType::class, array(
'label' => "Rayon d'intervention",
'required' => false
))
->add('picture', new DocumentType(), array('required' => false))
->add('curriculum', new DocumentType(), array('required' => false))
->add('motivation', new DocumentType(), array('required' => false))
->add('document', new DocumentType(), array('required' => false))
->add('abilities', 'entity', array(
'label' => 'Domaines de compétence',
'attr' => array(
'class'=>'form-control multiselect'
),
'class' => 'PixUserBundle:Ability',
'property' => 'title',
'multiple' => true,
'expanded' => true,
'required' => false,
'query_builder' => function(AbilityRepository $er) {
return $er->getAbilities();
},
))
->add('condition', 'checkbox', array('label' => "J'ai lu et j'accepte les conditions générales."))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Pix\UserBundle\Entity\User',
'intention' => 'registration'
));
}
public function getBlockPrefix()
{
return 'pix_userbundle_registrationformtype';
}
public function getParent()
{
return RegistrationFormType::class;
}
}
【问题讨论】:
-
删除这一行
use FOS\UserBundle\Form\Type\RegistrationFormType;- 通过声明您定义或使用名为RegistrationFormType的类的 use 语句 - 与此类同名,那么这部分return RegistrationFormType::class;是不明确的。 -
这是两种不同的形式,尽管它们看起来很相似,或者其中一个文件是旧版本并且不需要。无论哪种方式,为什么网站管理员在那里有一个具有相同类名的
use是没有意义的。FOS\UserBundle\Form\Type\RegistrationFormType存在吗?请记住,对于 Symfony,他们建议将每个表单放在自己的类中,但它们也应该有可区分的名称。 -
我遇到了同样的问题。修复命名空间后,错误消息消失了。