Symfony2 表单重复元素自定义标签
我正在使用 Symfony2 和 CraueFormFlowBundle 创建多步骤表单。除了我重复的电子邮件字段之外,一切都很顺利。为了我自己,我无法找到如何贴上我想要的标签。我正在使用 form_widget(...) 在 Twig 视图中自己渲染表单并编写标签。我按照客户的要求安排一切。现在,他希望看到电子邮件标签为“电子邮件*”和“确认电子邮件*”(星号,因为它们是必需的)。如果我使用 form_row() 渲染重复的元素,错误将不再显示在表单上(但我可以控制标签,snap)。显示错误的唯一方法(不要问我为什么)是使用 form_widget(form.giver.email) ,它指向整个重复的元素对象。问题是,使用 form_widget 渲染整个重复元素使我无法控制标签。
通过渲染整个重复元素,它使用“first_name”和“second_name”参数打印标签。由于显而易见的原因,我不能在这些参数中输入大写字母、破折号或星号。如果我尝试在选项数组中设置标签,该标签将传递到 Symfony2 文档中描述的两个字段...
我尝试使用 twig 中的“.first”和“.second”进行打印,但出现错误说明 FormView 中不存在这些。
现在我想要的就是能够分别设置两个标签!这是我当前的代码:
$builder->add('email', 'repeated', array(
'type' => 'email',
'first_name' => 'email',
'second_name' => 'confirm',
'invalid_message' => 'The e-mails you provided did not match.',
'error_bubbling' => false
));
这会将标签打印为“电子邮件”和“确认”。这里使用“options”数组:
$builder->add('email', 'repeated', array(
'type' => 'email',
'first_name' => 'email',
'second_name' => 'confirm',
'invalid_message' => 'The e-mails you provided did not match.',
'error_bubbling' => false,
'options' => array(
'label' => "TESTTT"
),
));
这会将“TESTTT”标签打印到两个重复字段。我能做些什么吗?如上所述,如果电子邮件不相等或为空,则使用 form_row() 不会显示表单提交时的错误。所以我只能使用 form_widget() 并渲染整个重复的对象。
预先感谢您的宝贵时间。
I'm using Symfony2 and CraueFormFlowBundle to create a multi-step form. Everything is going well except for the my repeated email field. I cannot, for the sake of me, find how to put the labels I want. I am rendering the form by myself in the Twig view using form_widget(...) and writing the labels. I position everything following what my client wants. Now, he wishes to see the email labels as "E-mail*" and "Confirm e-mail*" (the stars since they're required). If I render the repeated elements using form_row(), the errors are not displayed anymore on the form (but I have control over the labels, snap). The only way to errors are displayed (don't ask me why), is by using form_widget(form.giver.email) which points to the whole repeated element object. Issue is, using the form_widget to render the whole repeated element gives me no control over the labels.
By rendering the whole repeated element, it prints the labels using the "first_name" and "second_name" parameters. I cannot put capital letters nor dashes nor stars in these parameters for obvious reasons. If I try to set the label in the options array, that label is passed to both fields as described in the Symfony2 doc...
I tried printing using the ".first" and ".second" in twig, but I get an error stating that these don't exist in FormView.
Now all I want is to be able to set the two labels separately! Here is my current code:
$builder->add('email', 'repeated', array(
'type' => 'email',
'first_name' => 'email',
'second_name' => 'confirm',
'invalid_message' => 'The e-mails you provided did not match.',
'error_bubbling' => false
));
This prints the labels as "email" and "confirm". Here is using the "options" array:
$builder->add('email', 'repeated', array(
'type' => 'email',
'first_name' => 'email',
'second_name' => 'confirm',
'invalid_message' => 'The e-mails you provided did not match.',
'error_bubbling' => false,
'options' => array(
'label' => "TESTTT"
),
));
This will print "TESTTT" label to both repeated fields. Is there anything I can do about this? As mentionned above, using form_row() does not display the errors on form submission if the emails aren't equal or if they're blank. So I am constrained to using form_widget() and rendering the whole repeated object.
Thanks in advance for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
还有更简单正确的方法:
享受!
There is more easy and correct way:
Enjoy!
使用
Use
以下示例对我有用。
这里是“类型”声明:
这里是“twig”使用
The following example worked for me.
Here the 'type' declaration:
And here the 'twig' use
不正确,最好尝试一下
,您将能够使用:
is incorrect, better try
and you will be able to use:
尝试在翻译文件中添加字段名称和标签?
例如 CraueFormFlowBundle.en.yml
Try add the field name and label in the translations file?
E.g. CraueFormFlowBundle.en.yml
任何想知道为什么自定义重复表单输入不起作用的人,请检查一下:
将为您提供“第一”和“第二”密码,可以根据以下方式调用:
但这:
不会!
Anyone wondering why customization repated form inputs doesn't work, check this out:
will give you "first" and "second" passwords, which can be called according to:
But this:
WON'T!!!