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

11
我正在使用Symfony2和CraueFormFlowBundle创建多步骤表单。除了我的重复电子邮件字段之外,一切都进行得很顺利。我无论如何都找不到如何放置我想要的标签。我在Twig视图中使用form_widget(...)自己呈现表单并编写标签。我根据客户的要求定位所有内容。现在,他希望将电子邮件标签显示为“E-mail*”和“Confirm e-mail*”(星号表示它们是必需的)。如果我使用form_row()渲染重复元素,则错误不再显示在表单上(但我可以控制标签)。唯一显示错误的方法(别问我为什么),是使用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
    ));

这将标签打印为"email"和"confirm"。这里使用了"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()并渲染整个重复对象。
非常感谢您的时间。
7个回答

11

有更简单和正确的方法:

->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
     */
))

享受吧!


这在Symfony2.0上不起作用。它在主分支(>=2.1?)中,但不在2.0分支中。 - flu
1
是的,这仅适用于Symfony 2.1。在被接受的答案中提到的另一种方法在2.1中不再可行。 - smoove
太棒了,它像魔法一样运行!我的代码: $builder->add('password', 'repeated', array( 'type' => 'password', 'first_name' => "password", 'second_name' => "confirmpass", 'invalid_message' => "密码不匹配", 'first_options' => array('label' => '密码'), 'second_options' => array('label' => '重复密码'))); - thorinkor

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

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

5
如果您正在使用FormBuilder(或表单类型),则语法将是$formBuilder->get('passwordFieldName')->get('second')->setAttribute('label', '再次输入密码'); - Nick

4
以下示例对我有效:

以下示例对我有效。

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

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

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

-1

如果有人想知道为什么自定义重复表单输入不起作用,请看这个:

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

不会!!!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接