Removing / deleting form fields in Symfony2

I am working on an extension of the FosUserBundle registration form. I need to remove / delete the username field (because I use the email address as the username).

Is there a way to remove a field from the form I am distributing?

+5
source share
2 answers

If you want to remove / disable a field in your form view that extends FOSUser, you can do something like:

public function buildForm(FormBuilder $builder, array $options) 
{
    parent::buildForm($builder, $options);        

    $builder->remove('username');
}
+21
source

If you want to override the attributes of constraints, for example, you can do something like this:

<?php

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="username", column=@ORM\Column(nullable = true, unique = false ))
 *   })
 */
class User extends BaseUser {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}
+1
source

All Articles