2015-02-09 121 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 

}