0
votes

I have install FOSUserBundle in my Symfony project. Now I want to Remove Registration Form Field that by default provide by FOSUserBundle.

Registration Form Fields Are :

User Name

Email Id

Password

Repeat Password

Now I don't want Email Field when User are register so I override Registration form in my bundle.

\\ Front\FrontBundle\Form\RegistrationType.php
<?php
namespace Front\FrontBundle\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'); // here I code for remove email field.
   }



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

    // Or for Symfony < 2.8
    // return 'fos_user_registration';
  }

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

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

}

then I change config.yml and services.yml file

\\ App/config/config.yml

fos_user:
db_driver: orm 
firewall_name: main
user_class: Front\FrontBundle\Entity\User
registration:
    form:
        type: Front\FrontBundle\Form\RegistrationType




\\app/config/services.yml
services:
app.form.registration:
    class: Front\FrontBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }  

So After done with this Email Field remove from my Registration Form but when I submit form after filling username , password , repeat password it's give me any error that The email is not valid.

So I need to change any other file to remove email validation with email field ?

Thanks.

1
email is a required field of the model that FOSUser provide you to extends. If you don't want it, just don't extend FOS\UserBundle\Model\User. - Federkun
@Federico can you explain in detail? In which file I need to change? - Dhaval
Since email is defined in the abstraction provided from FOSUserBundle, you are pretty much forced to use it. I believe that the simple solution is to override the setUsername method of your model, and set a fake (valid and unique) email here. - Federkun
But if you don't need most of the features provided by FOSUserBundle, just don't use it. - Federkun

1 Answers

0
votes

you can remove the field as you have done, but you must make sure that there is no residual server side validation that requires it.

how to do this depends on how you've extended the FOS User class (or if you have).

annotated constraints look like this for instance (from the docs)

class Author
{
    /**
     * @Assert\NotBlank()           <--- remove this
     */
    public $name;
}
  • if youv'e extended the class, you can remove it from your own member definition.
  • If you've not extended it, extend it and then dont put in the validation constraint.
  • Or failing everything else (and I think this is hacky), issue a default in the controller before you call isValid().

    public function someAction(Request $request) {
        // ...
    
        $user = new User();
    
        // fill empty value
        $user->setEmail('[email protected]');
    
        // form stuff here
        // ...
        if ($form->isValid()) {
            // do some stuff
        }
    
        return $this->render(blahblahbal);
    }