Symfony窗体类:重命名字段名称属性的值(Symfony Form Class: rename fields name attribute's value)

编程入门 行业动态 更新时间:2024-10-21 11:41:14
Symfony窗体类:重命名字段名称属性的值(Symfony Form Class: rename fields name attribute's value)

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

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

现在我想问问是否可以重命名单个字段名称,并且表单仍然有效。 像这样的东西:

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

这可能吗?

更多的解释:这是我想要实现的: 带有窗体类的默认Symfony用户实体 我发现默认登录需要名为: _username和_password的输入字段,但表单不会_password该字段。 但是这个表格是有效的,它需要_token字段。

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

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

它会将所有名称转换为这种形式: _[field_names]对于令牌很有用,但不适合我的用户名和密码字段。 它的需要是_username和_password但它实际上是_[username]和_[password] ,这对我来说并不好,对于仅仅抛出Bad Credentials的Security Bundle也不是。

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

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:

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

So is this possible?

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 '_'; }

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.

最满意答案

你让事情变得复杂。 您的表单类对登录表单没有用处。 当你使用实体时,Form类是一个很好的工具,但你只需要提交两个字段并将它们传递给登录检查。 所以你不更新或创建一个实体。 但这是你实际需要表单类的地方。

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


你说你想学这个,但是你错误地学习它。 正如他们在这里建议您http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form你应该使用一个简单的树枝模板作为登录窗体类。


表单字段中的逻辑实际上是,该表单是对象,括号中的名称是属性,因此:

product[description]

是一个表单字段,表示表单处理“产品”实体,“描述”的特定字段要求您输入说明文本。


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

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.

Read the doc: http://symfony.com/doc/current/book/forms.html#creating-form-classes


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.


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

更多推荐

本文发布于:2023-04-29 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   字段   重命名   属性   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!