0

I have managed to override the template and the form for the registration page but one thing I have noticed is that the initial fields are being injected.

Here is my buildform Code:

public function buildForm(FormBuilderInterface $builder, array $options) {        
    $builder->add('firstname', 'text', array('label' => 'First Name'))
            ->add('lastname', 'text', array('label' => 'Last Name'))
            ->add('over_18', 'checkbox', array('label' => 'Yes I am over 18', 'mapped' => false))
            ->add('email', 'text', array('label' => 'Email'))
            ->add('phone', 'text', array('label' => 'What is your telephone number?'))
            ->add('password', 'password', array('label' => 'Choose a password'))
    ;
}

When I do a {{ dump(form) }} in the template I get this:

enter image description here

I understand I can do $builder->remove() on those fields, but if I am overwriting the form should I really need to do this?

Thanks

Wouter J
  • 41,455
  • 15
  • 107
  • 112
AntonioCS
  • 8,335
  • 18
  • 63
  • 92

2 Answers2

0

Yes. This is the only way. Because the buildForm() method is chained to parent buildForm(). That's how the form parent-child dependency works in symfony2:

/**
 * @return string
 */
public function getParent()
{
    return 'fos_user_registration';
}
Max Małecki
  • 1,700
  • 2
  • 13
  • 18
0

Not sure if this is the info you are looking for, but when using FOSUser and Sonata User, you can simply extend parent::buildForm() and then add your custom fields like this:

// src/Application/Sonata/UserBundle/Form/Type/RegisterType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // call to FOSUser buildForm, brings default fields
    parent::buildForm($builder, $options);

    $builder
        ->add('phone','text',array('label' => 'phone','required'=>true))
        ;
    //do $builder->remove(..) if necessary

}
Muhammed
  • 1,592
  • 1
  • 10
  • 18