When I did not want users to enter emails (thus, adding emails is optional to the FOSUserBundle), I use Symfony 2.7 + FOSUser + SonataUser + SonataAdmin.
At the same time, I needed the entered emails to be unique in the system. Thus, users have 2 options when registering:
- Leave your email blank
- Provide a unique email that is not already in the system.
Below, my solution works as expected (I do not claim that it is the cleanest, but I hope it shows you how to perform a similar task)
1) Changes in Entity / User.php
namespace AppBundle\Entity; use Sonata\UserBundle\Entity\BaseUser as BaseUser; use Doctrine\ORM\Mapping as ORM; class User extends BaseUser {
2) Completed doctrine app / console: migrations: diff and migrate, the database tables have been changed, as expected, adding "DEFAULT NULL" to the email and email_canonical fields
3) No matter what I tried, the email was set to NULL, but email_canonical was not, it returned. "I tried to manually set it to NULL in my RegisterFormHandler, var_dump there confirmed that it was really set to NULL when electronic no mail was entered, but I would send an βempty stringβ to the FOSUser database that violated the UNIQUE restriction that I set for emails, so the solution was to override the method in Entity / User.php (as discussed in previous answers to this question)
// src/AppBundle/Entity/User.php // ... public function setEmailCanonical($emailCanonical) { // when email is empty, force canonical to NULL // for some reason by default "" empty string is inserted $this->emailCanonical = $this->getEmail(); }
4) Change the validation for FOSUserBundle (or SonataUserBundle) in my case so that it does not require the installation of email. (I just deleted .. from validation.xml as not the ones that were applied to the email anymore)
Copy these 2 files to the config / validation / directory (for SonataUser + FOSUser this is: Application / Sonata / UserBundle / Resources)
- seller / friendsofsymfony / custom package / FOS / UserBundle / Resources / configuration / storage validation /orm.xml
- above path, config / validation / orm.xml
Rename the Registration group to these files in your name, for example, myRegistration.
Bind the new validation_group to fos_user in config.yml. If you are using Sonata User, this is:
sonata_user: profile: register: form: ... validation_groups: - myRegistration - Default
Enjoy.