sylzys
sylzys

Reputation: 167

Unable to override Sonata User Bundle registration form

I keep getting an error while trying to override sonata registration template.

I extended Sonata User Bundle with EasyExtendsBundle, so I now have src/Application/Sonata/UserBundle.

EDIT: Symfony 2.7, Sonata Admin 2.3, Sonata User dev-master

I added a field in my User Entity UserEntity.php

<?php

/**
 * This file is part of the <name> project.
 *
 * (c) <yourname> <youremail>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://sonata-project.org/bundles/easy-extends )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class User extends BaseUser
{
    /**
     * @var integer $id
     */
    protected $id;

   /*
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     */
     protected $age;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

     public function getAge()
    {
        return $this->age;
    }

        public function setAge($age)
    {
         $this->age = $age;
    }
}

I then created a new RegisterForm Application/Sonata/UserBundle/Form/Type/RegisterType.php

<?php

namespace Application\Sonata\UserBundle\Form\Type;

use FOS\UserBundle\Form\Type\RegistrationFormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegisterType extends RegistrationFormType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        parent::buildForm($builder, $options);

        $builder
            ->add('age');
    }

    public function setDefaultOption(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('Default', 'Register')
        ));
    }

    public function getName()
    {
        return 'front_user_registration';
    }
}

I tell sonata_user to use my form config.yml

sonata_user:
    profile:
        register:
            form:
                type:               front_user_registration
                handler:            sonata.user.profile.form.handler.default
                name:               front_user_registration_form

fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main

    # if you change the class configuration, please also alter the sonata_user.yml file
    user_class:     Application\Sonata\UserBundle\Entity\User

    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager

    service:
        user_manager: sonata.user.orm.user_manager
    registration:
        form:
            type: front_user_registration
    profile:
        form:
            type:               fos_user_profile
            handler:            fos_user.profile.form.handler.default
            name:               fos_user_profile_form
            validation_groups:  [Authentication]

which I declare as a service services.yml

user.form.register.type:
        class: Application\Sonata\UserBundle\Form\Type\RegisterType
        parent: fos_user.registration.form.type
        tags:
            - { name: form.type, alias: front_user_registration }

When I try to display the form, I get the following error :

Catchable Fatal Error: Argument 1 passed to Sonata\UserBundle\Form\Handler\ProfileFormHandler::process() must implement interface FOS\UserBundle\Model\UserInterface, boolean given, called in /Users/sylv/Sites/generajobs/vendor/sonata-project/user-bundle/Controller/RegistrationFOSUser1Controller.php on line 49 and defined

Same goes if I add

arguments: [%fos_user.model.user.class%]

into my services.yml configuration.

Am i missing something here ?

Upvotes: 0

Views: 2250

Answers (1)

sylzys
sylzys

Reputation: 167

That was a pretty stupid mistake, I had to change my "handler" line in config.yml to

handler:            sonata.user.registration.form.handler.default

instead of sonata.user.profile.form.handler.default as I saw on several examples on S.O.

The two handlers process() function do not except the same parameters.

Upvotes: 1

Related Questions