Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

Symfony FosUserBundle: Custom User Profile Form makes user registration not work

I have extended the ProfileFormType form in order to update an existing user profile:

namespace AppUserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class UserProfileFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name',TextType::class,array('label'=>'profile.first_name','required' => false));
        $builder->add('surname',TextType::class,array('label'=>'profile.surnname','required' => false));
        $builder->add('email',TextType::class,['required' => false]);
        $builder->add('description',TextareaType::class,['required' => false]);

    }


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

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

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

But for some reason when I perform the registration by performing an http post to register I get the following error message twice:

Please enter your name.

Do you have any Idea why that happens and ho I will solve it?

Edit 1

My Entity is:

namespace AppUserBundle\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;

    /**
     * @ORM\Column(name="user_image", type="string", length=255, nullable=true)
     */
    private $userImage;

    /**
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Please enter your 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"}
     * )
     */
    private $name;

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

    /**
     * @ORM\Column(name="description", type="string", length=1024, nullable=true)
     */
    private $description;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    public function setUserImage($imagepath)
    {
        $this->userImage=$imagepath;
    }

    public function functiongetUserImage()
    {
        return $this->userImage;
    }

    public function setName($name)
    {
        $this->name=strip_tags($name);
    }

    public function getName()
    {
        return $this->name;
    }

    public function setSurname($surname)
    {
        $this->surname=strip_tags($surname);
    }

    public function getSurname()
    {
        return $this->surname;
    }

    public function setDescription($description)
    {
        $this->description=$description;
    }

    public function getDescription()
    {
        return $this->description;
    }
}

Upvotes: 0

Views: 149

Answers (1)

gp_sflover
gp_sflover

Reputation: 3500

If your field is not empty (on form submit) you should check if in your Entity you've assigned some constraint to the name property like in the example below (using annotation):

  • @Assert\NotNull
  • @Assert\NotBlank.

Upvotes: 1

Related Questions