简体   繁体   English

Symfony2形成重复的元素自定义标签

[英]Symfony2 form repeated element custom labels

I'm using Symfony2 and CraueFormFlowBundle to create a multi-step form. 我正在使用Symfony2和CraueFormFlowBundle来创建一个多步骤表单。 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. 我正在使用form_widget(...)和编写标签在Twig视图中自己呈现表单。 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). 如果我使用form_row()渲染重复的元素,则表单上不再显示错误(但我可以控制标签,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. 显示错误的唯一方法(不要问我为什么),是通过使用指向整个重复元素对象的form_widget(form.giver.email)。 Issue is, using the form_widget to render the whole repeated element gives me no control over the labels. 问题是,使用form_widget渲染整个重复元素使我无法控制标签。

By rendering the whole repeated element, it prints the labels using the "first_name" and "second_name" parameters. 通过渲染整个重复元素,它使用“first_name”和“second_name”参数打印标签。 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... 如果我尝试在options数组中设置标签,那么该标签将传递给两个字段,如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. 我尝试使用twig中的“.first”和“.second”进行打印,但是我收到一条错误,指出这些在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. 这将在两个重复的字段上打印“TESTTT”标签。 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. 如上所述,如果电子邮件不相等或者它们是空白的,则使用form_row()不会在表单提交时显示错误。 So I am constrained to using form_widget() and rendering the whole repeated object. 所以我被限制使用form_widget()并渲染整个重复的对象。

Thanks in advance for your time. 在此先感谢您的时间。

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! 请享用!

{{ form_label(form.password.confirmpassword, 'Confirm Password') }}

采用

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

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') }} 
$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) }}

Try add the field name and label in the translations file? 尝试在翻译文件中添加字段名称和标签? Eg CraueFormFlowBundle.en.yml 例如CraueFormFlowBundle.en.yml

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!!! 惯于!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM