New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email only registration with unique validation on EmailCanonical #1516

Closed
wavyx opened this Issue Jun 23, 2014 · 26 comments

Comments

Projects
None yet
8 participants
@wavyx
Copy link

wavyx commented Jun 23, 2014

Hi,

I wonder if the Email unique constraint is applied for custom forms.
Or maybe I'm just struggling to configure FOSUserBundle with Email only registration without any username ;)

I switched to username_email provider

# app/config/security.yml
security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

adapted my User class:

    public function setEmail($email)
    {
        parent::setUsername($email);
        return parent::setEmail($email);
    }

    public function setEmailCanonical($emailCanonical)
    {
        parent::setUsernameCanonical($emailCanonical);
        return parent::setEmailCanonical($emailCanonical);
    }

Then created a custom form

class RegistrationFormType extends BaseType {

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

    public function getName()
    {
        return 'data_user_registration';
    }
} 

and added the following custom validation.xml

        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">emailCanonical</option>
            <option name="errorPath">email</option>
            <option name="message">fos_user.email.already_used</option>
            <option name="groups">
                <value>Registration</value>
                <value>Profile</value>
            </option>
        </constraint>

But I keep getting exceptions: duplicate key value violates unique constraint. As if the validation was not performed on the EmailCanonical value?!
Did I skip a configuration step?
Is there any documentation for this classic need: email + password only registration?
If not I'm willing to write a documentation section, once I get this working ;)

@wavyx wavyx changed the title Email only registration with unique validation on EmailCanonial Email only registration with unique validation on EmailCanonical Jun 23, 2014

@stof

This comment has been minimized.

Copy link
Member

stof commented Jun 23, 2014

which version of Symfony are you using ?

@stof

This comment has been minimized.

Copy link
Member

stof commented Jun 23, 2014

and which version of the bundle as well (use composer show -i friendsofsymfony/user-bundle to get all infos about the exact installed version)

@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 23, 2014

Symfony 2.5

name     : friendsofsymfony/user-bundle
descrip. : Symfony FOSUserBundle
keywords : User management
versions : * dev-master, 2.0.x-dev
type     : symfony-bundle
license  : MIT
source   : [git] https://github.com/FriendsOfSymfony/FOSUserBundle.git a93c355d70fd57a8171faece7969a13cb9387efb
dist     : [zip] https://api.github.com/repos/FriendsOfSymfony/FOSUserBundle/zipball/a93c355d70fd57a8171faece7969a13cb9387efb a93c355d70fd57a8171faece7969a13cb9387efb
names    : friendsofsymfony/user-bundle

autoload
psr-0
FOS\UserBundle => .

requires
php >=5.3.2
symfony/form ~2.1
symfony/framework-bundle ~2.1
symfony/security-bundle ~2.1

requires (dev)
doctrine/doctrine-bundle *
swiftmailer/swiftmailer >=4.3, <6.0
symfony/validator ~2.1
symfony/yaml ~2.1
twig/twig ~1.5
willdurand/propel-typehintable-behavior dev-master

suggests
willdurand/propel-typehintable-behavior Needed when using the propel implementation
@stof

This comment has been minimized.

Copy link
Member

stof commented Jun 23, 2014

Please update FOSUserBundle and retry. A fix related to the loading of validation constraints has been merged a few days ago, and a93c355 is a commit before the merge.
As a general rule, it is always good to check if the bundle can be updated when you face a bug. A fix might have been merged already.

@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 23, 2014

Upgraded to 71ed250
I'm still having duplicate insert exceptions. It seems related to username_canonical key.
But even if I use the following validation.xml the form is not validated and the exception is thrown

<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
        http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="Data\ModelBundle\Entity\User">
        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">usernameCanonical</option>
            <option name="errorPath">username</option>
            <option name="message">fos_user.username.already_used</option>
            <option name="groups">
                <value>Registration</value>
                <value>Profile</value>
            </option>
        </constraint>

        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">emailCanonical</option>
            <option name="errorPath">email</option>
            <option name="message">fos_user.email.already_used</option>
            <option name="groups">
                <value>Registration</value>
                <value>Profile</value>
            </option>
        </constraint>
    </class>

</constraint-mapping>
@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 23, 2014

I created a new project with only FOSUserBundle but still have the issue.
I started a documentation on how to configure "Email only registration"
https://github.com/wavyx/FOSUserBundle/blob/master/Resources/doc/email_only_registration.md

Do you see any problem in the configuration?

@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 23, 2014

pushed the sample project on https://github.com/wavyx/newdata

@adamelso

This comment has been minimized.

Copy link

adamelso commented Jun 25, 2014

I can confirm I'm having the same issue with Symfony 2.5. I was integrating with the HWIOAuthBundle, but just to be sure I hadn't messed something up, I started a fresh installation of Symfony 2.5 project, and followed the bare minimum installation of the bundle as provided in the docs. However, I still have the same issue.

I've implemented the following story for Behat:

  Background:
    Given there are following users:
      | email            | password  |
      | adam@example.com | foo       |

  Scenario: Trying to register with already existing email
    Given I am on the "Registration" page
    When I fill in the following:
      | Email         | adam@example.com |
      | Username      | Adam             |
      | Password      | bar              |
      | Verification  | bar              |
    And I press "Register"
    Then I should be on the Registration page
    And I should see "The email is already used"

And the exception that occurs each time the registration form is submitted.

An exception occurred while executing 'INSERT INTO UserAccount (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["Adam", "adam", "adam@example.com", "adam@example.com", 1, "ja37qlztei0oso8c4ksogogswgoggk8", "l/...Cm", null, 0, 0, null, null, null, "a:0:{}", 0, null]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'adam@example.com' for key 'UNIQ_A8761C51A0D96FBF'

Composer information:

$ composer show -i friendsofsymfony/user-bundle
name     : friendsofsymfony/user-bundle
descrip. : Symfony FOSUserBundle
keywords : User management
versions : * dev-master, 2.0.x-dev
type     : symfony-bundle
license  : MIT
source   : [git] https://github.com/FriendsOfSymfony/FOSUserBundle.git 71ed2505443f375876a38e82ff0d1929d6078270
dist     : [zip] https://api.github.com/repos/FriendsOfSymfony/FOSUserBundle/zipball/71ed2505443f375876a38e82ff0d1929d6078270 71ed2505443f375876a38e82ff0d1929d6078270
@stof

This comment has been minimized.

Copy link
Member

stof commented Jun 26, 2014

This is a bug in Symfony: symfony/symfony#11227

In the meantime, force the usage of the 2.4 validation API in your config

@adamelso

This comment has been minimized.

Copy link

adamelso commented Jun 26, 2014

@stof thank you :)

For reference to others, you'll need the configuration below, as noted in the update docs.

# app/config/config.yml
framework:
   validation:
       enabled: true
       api: 2.4
       enable_annotations: true # Provided already by when first installing Symfony
@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 27, 2014

@stof and @adamelso Thanks a lot for followup up quickly on this !

I've added the configuration in https://github.com/wavyx/newdata but I guess I'm missing a configuration step since I still got the exception

name     : friendsofsymfony/user-bundle
descrip. : Symfony FOSUserBundle
keywords : User management
versions : * dev-master, 2.0.x-dev
type     : symfony-bundle
license  : MIT
source   : [git] https://github.com/FriendsOfSymfony/FOSUserBundle.git 71ed2505443f375876a38e82ff0d1929d6078270
dist     : [zip] https://api.github.com/repos/FriendsOfSymfony/FOSUserBundle/zipball/71ed2505443f375876a38e82ff0d1929d6078270 71ed2505443f375876a38e82ff0d1929d6078270
names    : friendsofsymfony/user-bundle

autoload
psr-0
FOS\UserBundle => .

requires
php >=5.3.2
symfony/form ~2.1
symfony/framework-bundle ~2.1
symfony/security-bundle ~2.1

requires (dev)
doctrine/doctrine-bundle *
swiftmailer/swiftmailer >=4.3, <6.0
symfony/validator ~2.1
symfony/yaml ~2.1
twig/twig ~1.5
willdurand/propel-typehintable-behavior dev-master

suggests
willdurand/propel-typehintable-behavior Needed when using the propel implementation
@sloba88

This comment has been minimized.

Copy link

sloba88 commented Jun 30, 2014

I think I am having a same issue: unique constraints on email or username are not validating at all, it always break on database layer.
Other validation constraints seems to work. I am using symfony 2.5 and FOS User Bundle 71ed250

edit: works with forcing 2.4 validation

@stof

This comment has been minimized.

Copy link
Member

stof commented Jun 30, 2014

@sloba88 there is a bug in the new Symfony validation system. See my previous comment

@wavyx

This comment has been minimized.

Copy link

wavyx commented Jun 30, 2014

@sloba88 I've issued a pull request for a sample configuration. You can already check here https://github.com/wavyx/FOSUserBundle/blob/master/Resources/doc/email_only_registration.md

@weyandch

This comment has been minimized.

Copy link

weyandch commented Jul 30, 2014

@wavyx when overwriting the setEmail & setEmailCanonical methods in the user object (which seems be missing in your example for docs) i could simply stick to the regular userProvider, don't i?
Email & username are always in sync, thus default usernameProvider would work as excpected.. ?

@kcaporaso

This comment has been minimized.

Copy link

kcaporaso commented Aug 1, 2014

Forcing 2.4 also forces me to go back to Symfony\Component\Validator\Validator (deprecated in 2.5); I can't seem to use the newer ValidatorInterface. Is this expected and should we see some resolution commits soon?

Catchable fatal error: Argument 3 passed to MyUtil::__construct() must be an instance of Symfony\Component\Validator\Validator\ValidatorInterface, instance of Symfony\Component\Validator\Validator given, called in 

I'm injecting from services.yml with @validator

Am I overlooking something?
Thanks!

@merk

This comment has been minimized.

Copy link
Member

merk commented Aug 1, 2014

Where is MyUtil defined? This class does not exist in FOSUserBundle.

@kcaporaso

This comment has been minimized.

Copy link

kcaporaso commented Aug 1, 2014

@merk, that's my own class that I'm injecting the @validator into.

@merk

This comment has been minimized.

Copy link
Member

merk commented Aug 1, 2014

This isnt the appropriate place for symfony\validator support, please post a ticket in the user support areas for symfony2 (irc, forums, mailing lists, stack overflow)

@kcaporaso

This comment has been minimized.

Copy link

kcaporaso commented Aug 1, 2014

@merk I would think that if this is the side-effect of a bug's work around it's worth mentioning, no? I'm just sharing the wealth. Have a great day.

@wavyx

This comment has been minimized.

Copy link

wavyx commented Aug 12, 2014

@weyandch Indeed I forgot to mention I override the setEmail and setEmailCanonical to keep the username in sync (cfr https://github.com/wavyx/newdata/blob/master/src/Data/ModelBundle/Entity/DataUser.php)
I think this is still needed because my registration form only ask for email and password, so the username would be empty otherwise.

@stof

This comment has been minimized.

Copy link
Member

stof commented Aug 20, 2014

the bug in the 2.5 validation API has been fixed

@kcaporaso

This comment has been minimized.

Copy link

kcaporaso commented Sep 11, 2014

@stof So, in theory if I revert my config.yml, by removing the explicit force of API 2.4 validation and do a composer update (symfony 2.5.*) then unique emails should be getting caught again, correct?

@stof

This comment has been minimized.

Copy link
Member

stof commented Sep 29, 2014

yes.

@stuzzo

This comment has been minimized.

Copy link

stuzzo commented Apr 17, 2015

Hi, is possible that I have the same issue in Symfony 2.6.6?
In my registrationType I use as parent the fos_user_registration type and I added this

@UniqueEntity(fields="email", errorPath="email", message="fos_user.email.already_used", groups={"Registration", "Profile"})

Instead of the validation error, I receive error 500 for the duplicate entry.

@stof

This comment has been minimized.

Copy link
Member

stof commented Apr 18, 2015

@stuzzo no. symfony 2.6 does not suffer from this bug. If your validation is not applied, please open a dedicated issue.

I'm closing this issue as the bug was resolved in Symfony.

@stof stof closed this Apr 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment