Search code examples
formssymfonylabelelementcraueformflow

Symfony2 form repeated element custom labels


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.


Solution

  • Use

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