Question

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.

Was it helpful?

Solution

Use

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

OTHER TIPS

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

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? E.g. 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!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top