2015-02-09 124 views
0

我设法覆盖模板和注册页面的表单,但我注意到的一件事是最初的字段被注入。FOSUserBundle,SonataUserBundle覆盖注册

这是我buildform代码:

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

当我在模板做{{ dump(form) }}我得到这个:

enter image description here

我知道我可以做$ builder->删除()在这些领域,但如果我覆盖表单我应该真的需要这样做吗?

谢谢

回答

0

是的。这是唯一的方法。因为buildForm()方法被链接到父项buildForm()。这是形式的父子依赖于Symfony2的工作原理是:

/** 
* @return string 
*/ 
public function getParent() 
{ 
    return 'fos_user_registration'; 
} 
0

不知道这是你正在寻找,但使用FOSUser和索纳塔用户时,你可以简单地扩展父:: buildForm()和信息然后添加像这样的自定义字段:

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

}