FOSUserBundle树枝形式提交不预期的行为 [英] FOSUserBundle twig form submit not expected behaviour

查看:143
本文介绍了FOSUserBundle树枝形式提交不预期的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在FOSUserBundle之上使用PUGXMultiUserBundle来注册和登录两个不同的用户实体。



所有工作都在开箱即用:我有我的User类从FOSUserBundle扩展基本User类,为卖家和客户扩展我的User类。



我不希望我的用户输入他们的用户名,因为电子邮件是首选的登录属性。因此,在我的User类的setEmail()和setEmailCanonical()方法中,我还使用电子邮件设置了username和usernameCanonical。这很好,但是。



问题在于Twig。当我在表单末尾渲染一个form_rest(表单)时,它会正确提交并且用户被注册。但是,如果我尝试使用form_widget(form._token)呈现安全令牌,并提交,我最终以相同的形式,控制器不会被执行,数据库保持不变,不会返回错误。



((我这样做的原因不是显示用户名输入框,因为我不需要它给我的用户。))



所以问题是:除了_token隐藏输入,form_rest()呈现的是什么阻止我的表单正常工作?



有没有更好的方法为我想完成什么?



提前Thans。

div>

有两个步骤可以做到这一点:



1 事实证明,我已经发布了您的第一部分在删除/用电子邮件替换用户名字段在Symfony2中使用FOSUserBundle 。这是删除用户名的典型方法(让我们说让它沉默!)字段,只需 FOSUserBundle 。请遵循该帖子中提到的所有步骤。



2 在处理 PUGXMultiUserBunde :

  #PUGXMultiUserBundle 
pugx_multi_user:
db_driver:orm
用户:
user_one:
实体:
类:Acme\UserBundle\Entity\UserOne
注册:
形式:
类型:Acme\UserBundle\Form\Type\UserOneRegistrationFormType
名称:fos_user_registration_form
validation_groups:[AcmeRegistration,Default]
模板:AcmeUserBundle:注册:registerUserOne.html.twig

user_two:
实体:
类:Acme\UserBundle\Entity\UserTwo
注册:
形式:
类型:Acme\ UserBundle \Form\Type\UserTwoRegistrationFormType
名称:fos_user_registration_form
validation_groups:[AcmeRegistration,Default]
模板:AcmeUserBundle:注册:registerUserTwo.html.twig

应该就是这样!
$ b




编辑:验证组



AcmeRegistration 将包含FOSUserBundle中用于注册的所有约束,您可以将您不想申请(如用户名)。约束将在userOne和userTwo的公共字段中。



正如您在评论中提到的,您还可以创建:



AcmeUserOneRegistration 将包含所有UserOne注册特定的约束。



AcmeUserTwoRegistration 将包含特定于UserTwo注册的所有约束。



在PUGXMultiUserBundle配置中,对于userOne:


$ b $

  validation_groups:[AcmeRegistration,AcmeUserOneRegistration,Default] 

for userTwo:

  validation_groups:[AcmeRegistration,AcmeUserTwoRegistration,Default] 
code>


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.

解决方案

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]

这篇关于FOSUserBundle树枝形式提交不预期的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆