0

What is up, guys.

I'm using PUGXMultiUserBundle on top of FOSUserBundle to register and login two different user entities.

Everything is working out of the box: I have my User class extending the base User class from FOSUserBundle and my two entities for both Seller and Customer that extend my User class.

I don't want my users to input their username, as email is the prefered login property. So in the setEmail() and setEmailCanonical() methods of my User class, I also set username and usernameCanonical with the email. This works fine, BUT.

The issue is with Twig. When I render a form_rest(form) at the end of my form, it submits correctly and user gets registered. But if I try to render the security token with form_widget(form._token), and submit, I end up in the same form, controller doesn't get executed, database remains unaltered, errors are not returned.

((The reason I do this is not to show the "username" input box, as I don't require it to my users.))

So the question is: What does form_rest() render, besides the _token hidden input, that is preventing my form to work properly?

Is there a better approach for what I'm trying to accomplish?

Thans in advance.

Manuel Gómez
  • 103
  • 1
  • 9

1 Answers1

0

There are 2 steps to take to do that:

1 It turns out that I already posted the first part of your answer in Remove / Replace the username field with email using FOSUserBundle in Symfony2. It is the classical way to remove the username (let's say make it silent!) field, with just FOSUserBundle. Follow all the steps mentioned in that post.

2 There is an extra work that need to be done when working with PUGXMultiUserBunde:

#PUGXMultiUserBundle
pugx_multi_user:
  db_driver: orm
  users:
    user_one:
        entity:
          class: Acme\UserBundle\Entity\UserOne
        registration:
          form:
            type: Acme\UserBundle\Form\Type\UserOneRegistrationFormType
            name: fos_user_registration_form
            validation_groups:  [AcmeRegistration, Default]
          template: AcmeUserBundle:Registration:registerUserOne.html.twig

    user_two:
        entity:
          class: Acme\UserBundle\Entity\UserTwo
        registration:
          form:
            type: Acme\UserBundle\Form\Type\UserTwoRegistrationFormType
            name: fos_user_registration_form
            validation_groups:  [AcmeRegistration, Default]
          template: AcmeUserBundle:Registration:registerUserTwo.html.twig

That should be it!


Edit: Validation groups

AcmeRegistration will contain all the constraints that are present in FOSUserBundle for registration, and you can remove the ones you don't want to apply (like username). The constraints would be on common fields of both userOne and userTwo.

As you mention in your comment, you can also create:

AcmeUserOneRegistration will contain all the constraints specific for the registration of UserOne.

AcmeUserTwoRegistration will contain all the constraints specific for the registration of UserTwo.

In PUGXMultiUserBundle config, for userOne:

validation_groups:  [AcmeRegistration,AcmeUserOneRegistration, Default]

for userTwo:

validation_groups:  [AcmeRegistration,AcmeUserTwoRegistration, Default]
Community
  • 1
  • 1
Mick
  • 30,759
  • 16
  • 111
  • 130
  • Great! Thanks mate! I'm going to try it out right after lunch. Couple of things out of a brief reading, though: When you say Create a validation.yml file in Acme\UserBundle\Resources\config\config.yml, you mean in the config dir, right? And another thing: Should I have two different validation groups for the two different user entities? I assume so, although in your example above you use the same group for the two of them. Thanks again! :) – Manuel Gómez Jan 15 '14 at 12:34
  • 1yep in the config dir 2AcmeRegistration and AcmeProfile are actually replacing what has been done in FOSUserBundle, so they would be for fields like password, etc... If you want, you can create a AcmeUserOneRegistration validation group where you add all the constraints specific to the registration of user one (same with user two). See my edit. – Mick Jan 15 '14 at 13:50
  • Almost there! But I'm getting an Integrity constraint violation: 1048 Column 'username' cannot be null. I guess I'm not overriding the validations after all. Is it enough to have validations.yml in the Resources/config dir of my AcmeBundle? Or should it live in the app/Resources/config dir? Thanks! – Manuel Gómez Jan 15 '14 at 15:43
  • My doctrine:schema:create --dump-sql returns a CREATE TABLE sentence with a username VARCHAR(255) NOT NULL field, so there is something wrong there. – Manuel Gómez Jan 15 '14 at 16:43
  • We didn't remove the username field that is important I the FOSUserBUndle, we assign it the same value as the email (which is unique too). However, we'll never use it, we are just making it silent. So, see point nb 1 of the answer I mentioned, there is a setter to modify in your user entity 'setEmail'. – Mick Jan 15 '14 at 19:00
  • We didn't remove the username field that is important for the `FOSUserBUndle`, we assign it the same value as the `email` (which is unique too). However, we'll never use it, we are just making it silent. So, this step is the point nb 1 of the [answer I mentioned](http://stackoverflow.com/questions/8832916/remove-replace-the-username-field-with-email-using-fosuserbundle-in-symfony2/21064627#21064627) above... there is a setter to modify ('setEmail') in your user entity, and it will override the username `$this->username = $email'. – Mick Jan 15 '14 at 19:05
  • It is set, but still that is the output I get. I even overrode the setEmailCanonical method to set the usernameCanonical too. Weird. – Manuel Gómez Jan 15 '14 at 19:32
  • Your UserEntity in Acme bundle should extend the one in FOSUserBundle. Can you check that? You get a simple error.. can you try to see if your setter `setEmail($email)` is executed during registration by putting a `die()` statement or whatever you want. – Mick Jan 16 '14 at 11:56