famas23
famas23

Reputation: 2280

Remove email field FOSUserBundle

I need to remove email field from th registration form.
My solution was to override the registration FormType:

<?php
// src/AppBundle/Form/RegistrationType.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->remove('email');
    }

The filed is removed successfully but, the validation processus is fired "Please enter an email."
Any idea about how to disable the validation for email field or even how to make the trick with the right way.

Upvotes: 1

Views: 580

Answers (2)

Alexandre Tranchant
Alexandre Tranchant

Reputation: 4551

Hack the entity setter:

public function setUsername($username) {
    $username = is_null($username) ? '' : $username;
    parent::setUsername($username);
    $this->setEmail($username);
    return $this;
}

Remove the field from the FormType:

public function buildForm(FormBuilder $builder, array $options) {
    parent::buildForm($builder, $options);
    $builder->remove('email');
}

BUT before hack it, you should have a look to this presentation from jolicode.

If you are currently doing this kind of modifications, it is because FosUserBundle is not adapted to your project. I think you shouldn't use it. (Personnaly, I think this is not a good bundle, read the complete presentation above to make your own opinion)

If you want to replace it, I advice you to use this excellent tutorial to create your own security system. (Time to code/paste/understand it : 2 or 3 hours)

Upvotes: 1

hous
hous

Reputation: 2679

you can give it the value of username for example

User.php

* @ORM\HasLifecycleCallbacks


/**
 *
 * @ORM\PrePersist()
 */
public function setEmailUser(){

    $this->email = $this->username;
}

Upvotes: 1

Related Questions