Symfony 表单类:重命名字段名称属性的值 [英] Symfony Form Class: rename fields name attribute's value

查看:22
本文介绍了Symfony 表单类:重命名字段名称属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 FormBuilder 中设置单个表单字段名称?

Is there a way to set individual form fields name in the FormBuilder?

我的意思是通常你得到的是 form_name[field_name],或者如果 form_name 是一个空字符串你得到 field_name.

What I mean is normally you get is form_name[field_name], or if the form_name is an empty string you get field_name.

现在我想问一下是否可以重命名单个字段名称并且表单仍然有效.像这样:

Now I want to ask if it is possible to rename individual field name and the form still works. Something like this:

第一个字段 name="form_name[field_name]"
第二个字段 name="renamed_fildname"
第三个字段 name="form_name[field_name]"
等等...

First field name="form_name[field_name]"
Second field name="renamed_fildname"
Third field name="form_name[field_name]"
etc...

这可能吗?

更多解释:这就是我想要实现的目标:带有表单类的默认 Symfony 用户实体
我发现默认登录需要名为:_username_password 的输入字段,但表单没有构建它.但要使表单有效,它需要 _token 字段.

More explanation: this is what I am trying to achieve: Default Symfony User Entity with Form Class
I Figured out that the default login requires the input field named: _username and _password, but the form doesn't build that. But the form to be valid its needs the _token field.

现在,如果我尝试向表单添加名称:

Now if I try to add a name to the form:

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

它会将所有名称转换为这种形式:_[field_names] 这对令牌有好处,但对我的用户名和密码字段不利.它需要是 _username_password 但实际上是 _[username]_[password]对我不利,也不适合只会向我抛出错误凭据的安全捆绑包.

it will but all the names into this form: _[field_names] which is good for the token but not good for my username and password field. its need to be _username and _password but it actually is _[username] and _[password] which isn't good for me, nor for the Security Bundle which simply throws me Bad Credentials.

推荐答案

你让事情变得复杂.您的表单类对于登录表单毫无用处.当您处理实体时,表单类是一个很好的实用程序,但您只需要提交两个字段并将它们传递给登录检查.因此,您无需更新或创建实体.但这正是您真正需要表单类的地方.

You make things way to complicated. Your form class is useless for the login form. Form classes are a good utility when you work with entities, but you need just to submit two fields and pass them to the login check. So you don't update or create an entity. But this is where you actually need a form class.

阅读文档:http://symfony.com/doc/current/book/forms.html#creating-form-classes

你说你想学这个,但你学错了.正如他们在这里建议您 http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form 你应该使用一个简单的 twig 模板作为登录表单和无表单类.

You said you want to learn this but you learn it the wrong way. As they advise you here http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form you should use a simple twig template as login form an no form class.

表单域中的逻辑实际上是,表单是对象,括号中的名称是属性,所以:

The logic in the form field actually is, that form is the object and the name in the brackets is the attribute so:

product[description]

是一个表单字段,表明该表单正在处理产品"实体,而描述"的特定字段希望您输入描述文本.

is a form field that shows that the form is dealing with the "product" entity and the specific field which is "description" wants you to input a description text.

使用 FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle 不要重新发明轮子 ;)

Use FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle don't reinvent the wheel ;)

这篇关于Symfony 表单类:重命名字段名称属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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