简体   繁体   中英

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

I'm trying to extend the registration form to show more fields, but after trying multiple variations, I think either there's a bug, or the configuration settings I'm seeing on tutorials and posts are not correct for symfony 2.7 it's driving me nuts, thinking maybe wait till I upgrade to version 3.4, but upgrade isn't going smoothly so far.

error -

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

Form -

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 }

So as mentioned it's symfony 2.7 and Sonata user-bundle 3.2 any help would be appreciated with this one

The part of code where it errors is this line in 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"

# prefix: /register

full config

    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)

If you want to make it simply, you have to override the correct base registration form, from FOS\\UserBundle , like the 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();
   }
}

Then you have to configure your form type as a service :

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

Finally, you must update the configuration of the 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

Hope this will helps you...

Nothing from above worked in Symfony 2.7 for me. I upgraded to Symfony 3.4 and it now works!! perfectly following the standard guide https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html an upgrade really was needed, probably the autowiring features helped.

Try that:

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM