0

我试图覆盖 FOSUserBundle 的唯一原因是我可以在构建表单时放置一个默认值,这样我就不会在加载表单时收到不适当的验证消息......

我正在关注文档。我已经投入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 在第 95 行

        if (!$type) {
            throw new FormException(sprintf('Could not load type "%s"', $name));
        }
        $this->resolveAndAddType($type);
4

2 回答 2

3

问题是我的依赖注入文件夹中没有 EtienneUserExtension.php 文件(不正确的捆绑安装),因此无法加载 services.xml

于 2012-12-18T16:00:20.213 回答
1

在这段代码中,您首先BaseType从 Symfony 的组件中导入类,然后为fromForm定义具有相同名称的别名。RegistrationFormTypeFOS

 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;
于 2012-12-12T23:34:24.537 回答