11

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.

doydoy44
  • 5,720
  • 4
  • 29
  • 45

7 Answers7

11

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!

Andrey Chernykh
  • 146
  • 1
  • 4
  • This doesn't work with Symfony2.0. It's in the master branch (>=2.1?) but not in the 2.0 branch. – flu Jun 27 '12 at 14:27
  • 1
    Yes, this only works with Symfony 2.1. The other method mentioned in the accepted answer does not work anymore in 2.1. – smoove Aug 24 '12 at 12:52
  • great, it works like a charm! My code: $builder->add('password', 'repeated', array( 'type' => 'password', 'first_name' => "password", 'second_name' => "confirmpass", 'invalid_message' => "passwords don't match", 'first_options' => array('label' => 'password'), 'second_options' => array('label' => 'repeat password'))); – thorinkor Apr 17 '13 at 07:24
6
{{ form_label(form.password.confirmpassword, 'Confirm Password') }}
pmr
  • 58,701
  • 10
  • 113
  • 156
Kamlesh Kumar
  • 61
  • 1
  • 2
5

Use

$formView->getChild('passwordFieldName')->getChild('second')->set('label', 'Enter password again');
artemiuz
  • 444
  • 5
  • 7
  • 5
    If one is working within the `FormBuilder` (or Form Types) the syntax would be `$formBuilder->get('passwordFieldName')->get('second')->setAttribute('label', 'Enter password again');` – Nick Jul 15 '12 at 16:39
4

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') }}
    
Ralph KEMPS
  • 173
  • 1
  • 7
2
$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) }}
0

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

Grug
  • 1,870
  • 5
  • 25
  • 38
-1

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