FOSUserBundle twig form submit not expected behaviour

邮差的信 提交于 2019-12-13 07:46:57

问题


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.


回答1:


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]


来源:https://stackoverflow.com/questions/21136201/fosuserbundle-twig-form-submit-not-expected-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!