使用更多表单字段扩展 sonata 用户包,获取无法加载类型 "Application\Sonata\UserBundle\Form\RegistrationType"

extending sonata user bundle with more form fields, get Could not load type "Application\Sonata\UserBundle\Form\RegistrationType"

我正在尝试扩展注册表以显示更多字段,但在尝试多种变体之后,我认为要么存在错误,要么我在教程和帖子中看到的配置设置对于 symfony 2.7 不正确这让我抓狂,想也许等到我升级到 3.4 版,但到目前为止升级并不顺利。

错误 -

Could not load type 
                  "Application\Sonata\UserBundle\Form\RegistrationType"

表格 -

namespace Application\Sonata\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array 
$options)
{

    $builder->add('firstname')
        ->add('dateOfBirth');
}

public function getParent()
{
    return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}

public function getBlockPrefix()
{
    return 'app_user_registration';
}

// For Symfony 2.x
public function getName()
{
    return $this->getBlockPrefix();
}

}

config_dev.yml

  fos_user:
      db_driver: orm 
      firewall_name: secured_area
      user_class: Application\Sonata\UserBundle\Entity\User
      registration:
          form:
             type: Application\Sonata\UserBundle\RegistrationType
      group:
         group_class: Application\Sonata\UserBundle\Entity\User

services.yml

        services:
          app.form.registration: 
          class: Application\Sonata\UserBundle\Form\RegistrationType
          arguments: [%fos_user.model.user.class%]
          tags:
          - { name: form.type, alias: app_user_registration }

如前所述,它是 symfony 2.7 和 Sonata user-bundle 3.2 任何帮助都将不胜感激

错误的代码部分是 config_dev.yml

中的这一行
    registration:
      form:
         type: Application\Sonata\UserBundle\RegistrationType

routing.yml

       fos_user_register:
       resource: 
       "@FOSUserBundle/Resources/config/routing/registration.xml"
       prefix: /register

    #  sonata_user_register:
    # resource: 

   # 

 @SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"

#前缀:/register

完整配置

    fos_user:
             db_driver: orm # other valid values are 
             'mongodb', 'couchdb' and 'propel'
              firewall_name: secured_area
              registration:
              form:
              type: eventsBundle\Form\RegistrationType
              user_class: 
              Application\Sonata\UserBundle\Entity\User
              group:
              group_class: Application\Sonata\UserBundle\Entity\User
              #group_manager: sonata.user.orm.group_manager                    
              # If you're using doctrine orm (use 
              Sonata.user.mongodb.group_manager for mongodb)

              service:
              user_manager: sonata.user.orm.user_manager                      
              # If you're using doctrine orm (use 
              sonata.user.mongodb.user_manager for mongodb)

试试看:

config.yml

registration:
    form:
        type: eventsBundle\Form\RegistrationType

service.yml

app.form.registration:
    class: eventsBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }

如果你想简单点,你必须覆盖正确的基础注册表,从FOS\UserBundle,像Official FOSUSerBundle documentation :

namespace YourBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;

use YourBundle\Entity\User;

class RegistrationType extends BaseType 
{
   public function __construct()
   {
      parent::__construct(User::class);
   }  
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
      parent::buildForm($builder, $options);
      $builder->add('firstname')
              ->add('dateOfBirth');
   }
   public function getParent()
   {
       return 'FOS\UserBundle\Form\Type\RegistrationFormType';
       // Or for Symfony < 2.8
       // return 'fos_user_registration';
   }
   public function getBlockPrefix()
   {
       return 'app_user_registration';
   }
   // For Symfony 2.x
   public function getName()
   {
       return $this->getBlockPrefix();
   }
}

然后您必须将表单类型配置为服务:

# app/config/services.yml
services:
    app.form.registration:
        class: YourBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }

最后,您必须更新 FOSUserBundle 的配置:

# app/config/config.yml
fos_user:
    # ...
    registration:
        form:
            type: YourBundle\Form\RegistrationType
            # if you are using Symfony < 2.8 you should use the type name instead
            # type: app_user_registration

希望这对您有所帮助...

上面的任何东西对我来说在 Symfony 2.7 中都不起作用。我升级到 Symfony 3.4,现在可以使用了!!完全遵循标准指南 https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html 确实需要升级,自动装配功能可能有所帮助。