FOSUser register form is customizable: data not saved in DB

I am trying to set up the FOSUserBundle registration form the first time following the documentation , but when I submit the form there is an error message that asks me to "enter first name" even if the field is not blank.

I am wrong about something and I do not know what. Here is my code:

src/AppBundle/Entity/User.php

:

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=255)
     * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
     protected $firstname;

        // ...

     public function getFirstname()
     {
         return $this->firstname;
     }
 
     public function setFirstname($firstname)
     {
         $this->$firstname = $firstname;
     }

        // ...
}
      

Run codeHide result


src/AppBundle/Form/RegistrationType.php

:

<?php

namespace AppBundle\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', null, array(
            'label' => false,
            'translation_domain' => 'FOSUserBundle',
            'attr' => array(
                'placeholder' => 'form.firstname'
                )
            ));
    }

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

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

      

Mine app/config/services.yml

and are app/config/config.yml

exactly the same as in the documentation.

What I did wrong? Thank.

EDIT:

After some research, it looks like the form is not filling the object User

. If the field is firstname

filled Jason

(for example) it will add a key to the named object Jason

and populated with Jason

and firstname

will null

.

BUT if the field is filled with firstname

SO the key firstname

will be filled firstname

and it will be saved to the DB: If it is full If it is filled with a name

+3


source to share


1 answer


Try specifying a force check group. In your RegistrationType class, you can override the configureOptions method:



    /**
     * @param OptionsResolver $resolver
     */
     public function configureOptions(OptionsResolver $resolver)
     {
        $resolver->setDefaults(array(
        'data_class'            => 'AppBundle\Entity\User', // not required
        'csrf_protection'       => true, // not required
        'validation'            => true, // not required
        'validation_groups'     => ['Registration', 'Profile'], // try this! 
        ));
     }

      

+1


source







All Articles