2011-06-22 42 views
5

Vì vậy, tôi cố gắng nhúng một biểu mẫu vào một biểu mẫu khác mà không cần tạo lớp cho biểu mẫu. Heres những gì ive cóSymfony2 Subforms

$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(); 

Tuy nhiên, trên getForm() chức năng, nó nói

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

ai có bất kỳ ý tưởng cách im trù được này để làm việc sử dụng các công cụ quickloading?

heres 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

Là mã điều khiển này? Hoặc mã loại biểu mẫu tùy chỉnh? –

+0

trong bộ điều khiển, xin lỗi – Ascherer

+0

Và phương thức 'buildForm' được định nghĩa ở đâu? Bạn có thể đăng mã nguồn của nó không? –

Trả lời

4

Đã kết thúc việc tạo ra một giao diện cho các subform và thêm nó như thế này

$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(); 

nếu có ai có thể tìm thấy một cách dễ dàng hơn mà không cần tạo giao diện, cho tôi biết

+0

tôi sẽ nói bạn không cần' getForm() 'là' $ form' đã là biểu mẫu của bạn. 'getForm()' được sử dụng với 'FormBuilder' phải không? –

+0

'buildForm' đang trả về' Trình tạo biểu mẫu' – Ascherer

0

Hãy thử

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

Nếu bạn vượt qua một đối tượng FormBuilder cho phương thức add mà không chỉ định tên thuộc tính, nó sử dụng tên của đối tượng FormBuilder (đó là 'form') làm tên thuộc tính. Không có thuộc tính nào có tên form trong thực thể UserAlert.

+0

alright, đó là không làm việc, nhưng khi tôi lật nó, '$ form-> thêm ($ subform, 'userAlertConfig');' nó được cho tôi một chút gần gũi hơn. bây giờ im gặp vấn đề với proxy lol 'dự kiến ​​đối số kiểu "\ Neokeo \ MgmtBundle \ Entity \ UserAlert", "Proxies \ NeokeoMgmtBundleEntityUserAlertConfigProxy" given' – Ascherer