Symfony2 表单重复元素自定义标签

发布于 2024-12-14 06:22:44 字数 1523 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

闻呓 2024-12-21 06:22:44

还有更简单正确的方法:

->add('plainPassword', 'repeated', array(
    'type' => 'password',
    'invalid_message' => "some.error.message",
    'first_name' => 'somecoorectname', // (optional). 'first' value by default. 
    'first_options' => array(
        'label' => 'label.for.future.translate' // Custom label for element 
    )
    /*
       The same way for Second field
     */
))

享受!

There is more easy and correct way:

->add('plainPassword', 'repeated', array(
    'type' => 'password',
    'invalid_message' => "some.error.message",
    'first_name' => 'somecoorectname', // (optional). 'first' value by default. 
    'first_options' => array(
        'label' => 'label.for.future.translate' // Custom label for element 
    )
    /*
       The same way for Second field
     */
))

Enjoy!

似狗非友 2024-12-21 06:22:44
{{ form_label(form.password.confirmpassword, 'Confirm Password') }}
{{ form_label(form.password.confirmpassword, 'Confirm Password') }}
飘过的浮云 2024-12-21 06:22:44

使用

$formView->getChild('passwordFieldName')->getChild('second')->set('label', 'Enter password again');

Use

$formView->getChild('passwordFieldName')->getChild('second')->set('label', 'Enter password again');
写给空气的情书 2024-12-21 06:22:44

以下示例对我有用。

  • 这里是“类型”声明:

    $builder->add('用户名', '文本')
            -> 添加('电子邮件', '电子邮件')
            ->add('密码', '重复', 
            数组('类型' => '密码'));
    
  • 这里是“twig”使用

    {{ form_errors(form.password.first) }}
    {{ form_widget(form.password.first) }}
    {{ form_label(form.password.first, '密码') }}
    
    {{ form_errors(form.password.second) }}
    {{ form_widget(form.password.second) }}
    {{ form_label(form.password.second, '确认密码') }}
    

The following example worked for me.

  • Here the 'type' declaration:

    $builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
            array('type' => 'password'));
    
  • And here the 'twig' use

    {{ form_errors(form.password.first) }}
    {{ form_widget(form.password.first) }}
    {{ form_label(form.password.first, 'Password') }}
    
    {{ form_errors(form.password.second) }}
    {{ form_widget(form.password.second) }}
    {{ form_label(form.password.second, 'Confirm password') }}
    
相权↑美人 2024-12-21 06:22:44
$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'Password', 
                'second_name' =>'Confirm password'));

不正确,最好尝试一下

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'first', 
                'second_name' =>'second'));

,您将能够使用:

{{ form_errors(form.password.first) }}
{{ form_widget(form.password.first) }}
{{ form_label(form.password.first) }}

{{ form_errors(form.password.second) }}
{{ form_widget(form.password.second) }}
{{ form_label(form.password.second) }}
$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'Password', 
                'second_name' =>'Confirm password'));

is incorrect, better try

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'first', 
                'second_name' =>'second'));

and you will be able to use:

{{ form_errors(form.password.first) }}
{{ form_widget(form.password.first) }}
{{ form_label(form.password.first) }}

{{ form_errors(form.password.second) }}
{{ form_widget(form.password.second) }}
{{ form_label(form.password.second) }}
吹梦到西洲 2024-12-21 06:22:44

尝试在翻译文件中添加字段名称和标签?
例如 CraueFormFlowBundle.en.yml

Try add the field name and label in the translations file?
E.g. CraueFormFlowBundle.en.yml

闻呓 2024-12-21 06:22:44

任何想知道为什么自定义重复表单输入不起作用的人,请检查一下:

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated');

将为您提供“第一”和“第二”密码,可以根据以下方式调用:

{{ form_errors(form.password.first) }}
{{ form_widget(form.password.first) }}
{{ form_label(form.password.first) }}

{{ form_errors(form.password.second) }}
{{ form_widget(form.password.second) }}
{{ form_label(form.password.second) }}

但这:

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'Password', 
                'second_name' =>'Confirm password'));

不会!

Anyone wondering why customization repated form inputs doesn't work, check this out:

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated');

will give you "first" and "second" passwords, which can be called according to:

{{ form_errors(form.password.first) }}
{{ form_widget(form.password.first) }}
{{ form_label(form.password.first) }}

{{ form_errors(form.password.second) }}
{{ form_widget(form.password.second) }}
{{ form_label(form.password.second) }}

But this:

$builder->add('username', 'text')
            ->add('email', 'email')
            ->add('password', 'repeated', 
             array('type' => 'password', 
                'first_name'=>'Password', 
                'second_name' =>'Confirm password'));

WON'T!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文