2011-06-22 64 views
5

所以,即時嘗試嵌入一個窗體在另一個沒有創建一個窗體的類。下面有什麼Ive得到Symfony2子表格

$form = $this 
     ->buildForm('UserAlert', $alert) 
     ->add('Alert', 'entity', array('class' => 'Blah\MgmtBundle\Entity\Alert', 'property' => 'name', 'required' => true)) 
     ->add('Site', 'entity', array('class' => 'Blah\MgmtBundle\Entity\Site', 'property' => 'name', 'required' => false)) 
     ->add('Keyword', 'entity', array('class' => 'Blah\MgmtBundle\Entity\Keyword', 'property' => 'name', 'required' => false)) 
     ->add('Variant', 'entity', array('class' => 'Blah\MgmtBundle\Entity\Variant', 'property' => 'name', 'required' => false)) 
     ->add('Name', 'text'); 


    $uac = $alert->getUserAlertConfig(); 
    $subform = $this 
     ->buildForm('UserAlertConfig', $uac) 
     ->add('EmailAlert', 'choice', array('choices' => array('1' => 'Yes', '0' => 'No'), 'required' => true, 'label' => 'Email Alerts')) 
     ->add('EmailHours', 'text', array('required' => false, 'label' => 'Email Alert Hours')) 
     ->add('TextAlert', 'choice', array('choices' => array('1' => 'Yes', '0' => 'No'), 'required' => true, 'label' => 'Text Alerts')) 
     ->add('TextHours', 'text', array('required' => false, 'label' => 'Text Alert Hours')); 

    $form->add($subform); 
    $form = $form->getForm(); 

然而,在getForm()功能,它說

Neither property "form" nor method "getForm()" nor method "isForm()" exists in class "Blah\MgmtBundle\Entity\UserAlert" 

人有任何想法IM應該如何得到這個使用quickloading東西工作?

繼承人buildForm

public function buildForm($model = '', $data) 
{ 
    if (empty($model)) { 
     throw new \Exception("Must define a model"); 
    } 
    return $this->get('form.factory')->createBuilder('form', $data, array('data_class' => "\\Blah\\MgmtBundle\\Entity\\$model")); 
} 


    Stack Trace 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php at line 314  
at PropertyPath ->readProperty (object(UserAlert), '0') 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php at line 191  
at PropertyPath ->getValue (object(UserAlert)) 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 64 
at PropertyPathMapper ->mapDataToForm (object(UserAlert), object(Form)) 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 55 
at PropertyPathMapper ->mapDataToForms (object(UserAlert), array('_token' => object(Form), 'Alert' => object(Form), 'Site' => object(Form), 'Keyword' => object(Form), 'Variant' => object(Form), 'Name' => object(Form), 'form' => object(Form))) 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/Form.php at line 404  
at Form ->setData (object(UserAlert)) 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Component/Form/FormBuilder.php at line 659  
at FormBuilder ->getForm() 
in /mnt/www/reporting/src/Blah/MgmtBundle/Controller/AlertController.php at line 96  
at AlertController ->editAction ('1') 
in at line  
at call_user_func_array (array(object(AlertController), 'editAction'), array('1')) 
in kernel.root_dir/bootstrap.php.cache at line 438  
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/bootstrap.php.cache at line 416  
at HttpKernel ->handle (object(Request), '1', true) 
in /mnt/www/reporting/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php at line 44  
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 612  
at Kernel ->handle (object(Request)) 
in /mnt/www/reporting/web/app_dev.php at line 12  
+0

這是控制器代碼?或自定義表單類型代碼? –

+0

它在控制器中,對不起 – Ascherer

+0

而'buildForm'方法是在哪裏定義的?你能發佈它的源代碼嗎? –

回答

4

結束了創建的子窗體的接口,並加入像這樣

$form = $this 
     ->buildForm('UserAlert', $alert) 
     ->add('Alert', 'entity', array('class' => 'Neokeo\MgmtBundle\Entity\Alert', 'property' => 'name', 'required' => true)) 
     ->add('Site', 'entity', array('class' => 'Neokeo\MgmtBundle\Entity\Site', 'property' => 'name', 'required' => false)) 
     ->add('Keyword', 'entity', array('class' => 'Neokeo\MgmtBundle\Entity\Keyword', 'property' => 'name', 'required' => false)) 
     ->add('Variant', 'entity', array('class' => 'Neokeo\MgmtBundle\Entity\Variant', 'property' => 'name', 'required' => false)) 
     ->add('Name', 'text'); 

    $uac = $alert->getUserAlertConfig(); 



    $subform = $this->buildForm('UserAlertConfig', $uac, new \Neokeo\MgmtBundle\Form\UserAlertConfig) 
     ->add('EmailAlert', 'choice', array('choices' => array('0' => 'No', '1' => 'Yes'), 'required' => true, 'label' => 'Email Alerts')) 
     ->add('TextAlert', 'choice', array('choices' => array('0' => 'No', '1' => 'Yes'), 'required' => true, 'label' => 'Text Alerts')); 

    $form->add($subform, '', array('label' => '')); 
    $form = $form->getForm(); 

如果任何人都可以找到一個更簡單的方法,而無需創建界面,讓我知道

+0

我會說你不應該需要'getForm()''作爲一個問題$ form'已經是你的形式了。 getForm()與FormBuilder一起使用不是嗎? –

+0

'buildForm'返回一個'FormBuilder' – Ascherer

0

嘗試

$form->add('The name of the UserAlertConfig property in the UserAlert entity', $subform); 

如果傳遞一個FormBuilder對象add方法沒有指定屬性的名稱時,它使用FormBuilder對象的名稱(這是'form')作爲屬性名稱。 UserAlert實體中沒有名爲form的屬性。

+0

好吧,那不是工作,但是當我把它翻轉時,'$ form-> add($ subform,'userAlertConfig');'它讓我更靠近一點。 現在即時通訊具有代理笑 '型的預期參數 「\ Neokeo \ MgmtBundle \實體\ UserAlert」, 「代理\ NeokeoMgmtBundleEntityUserAlertConfigProxy」 given' – Ascherer