2012-05-13 27 views
5

Tôi đang tạo biểu mẫu bằng trình tạo biểu mẫu của Sf2.chỉnh sửa Symfony2 thực thể lớn dưới dạng tab

public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder->add('firstName') 
      ->add('lastName')... 

Thực thể có nhiều trường và tôi muốn đặt chúng trong tab giao diện người dùng jQuery. Nhưng trong mẫu hình tôi muốn sử dụng lệnh đơn lẻ

<form action="#" method="post" {{ form_enctype(form) }}> 
    {{ form_widget(form) }} 
    <input type="submit" value="Save"/> 
</form> 

Giải pháp tốt nhất là gì?

chỉnh sửa **

Để có conrete hơn: Tôi có 4 lĩnh vực: firstName, lastName, ngày sinh, deathDate. Tôi muốn 2 trường đầu tiên xuất hiện trên tab đầu tiên và 2 trường cuối cùng sẽ nằm trên tab thứ hai. Tôi muốn giữ cách vẽ biểu mẫu như đã đề cập trước đó.

Tôi mặc dù một giải pháp để tạo trường của riêng tôi không conneceted để underlaying đối tượng mà sẽ làm cho các thẻ html yêu cầu (h3, div, vv).

Trả lời

3

tôi xác định lĩnh vực của riêng tôi gọi là 'Tab' và thêm nó khi tab mới sẽ xuất hiện.

<?php 
//\src\Alden\xyzBundle\Form\Type\TabsType.php 

namespace Alden\BonBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use Symfony\Component\Form\FormView; 
use Symfony\Component\Form\FormInterface; 
use Symfony\Component\Form\FormError; 
use Symfony\Component\Form\CallbackValidator; 
use Symfony\Component\Form\FormValidatorInterface; 
use Symfony\Component\Form\Form; 

class TabsType extends AbstractType { 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->setAttribute('starting', $options['starting']); 
     $builder->setAttribute('ending', $options['ending']); 
     $builder->setAttribute('header', $options['header']); 
    } 

    public function buildView(FormView $view, FormInterface $form) 
    { 
     $parent = $form->getParent(); 
     if (is_null($parent->getParent())) 
     { 
      $tabs = $this->findTabs($parent); 
     } 
     else 
     { 
      $tabs = array(); 
     } 
     $view->set('starting', $form->getAttribute('starting')); 
     $view->set('ending', $form->getAttribute('ending')); 
     $view->set('header', $form->getAttribute('header')); 
     $view->set('tabs', $tabs); 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'property_path' => false, 
      'starting' => true, 
      'ending' => true, 
      'header' => false, 
     ); 
    } 

    public function getName() 
    { 
     return 'tabs'; 
    } 

    public function getParent(array $options) 
    { 
     return 'field'; 
    } 

    private function findTabs(Form $form) 
    { 
     $prefix = $form->getName(); 
     $tabs = array(); 
     foreach ($form->getChildren() as $child) 
     { 
      foreach ($child->getTypes() as $type) 
      /* @var $child \Symfony\Component\Form\Form */ 
      { 
       if (get_class($type) == __NAMESPACE__ . '\TabsType') 
       { 
        if ($child->getAttribute('starting')) 
        { 
         $tabs[$prefix . '_' . $child->getName()] = $child->getAttribute('label'); 
        } 
       } 
      } 
     } 
     return $tabs; 
    } 

} 

?> 

và cành

{# \src\Alden\xyzBundle\Resources\views\Form\fields.html.twig #} 
{% block tabs_row %} 
{% if header %} 
<ul> 
    {% for tid, t in tabs %} 
     <li> 
      <a href="#{{ tid }}">{{ t }}</a> 
     </li> 
    {% endfor %} 
</ul> 
{% endif %} 
{% if ending %} 
</div> 
{% endif %} 
{% if starting %} 
<div id="{{ id }}"> 
{% endif %} 
{% endblock %} 

và sử dụng theo hình thức xây dựng:

public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder 
      ->add('tabs_head', new TabsType(), array(
       'ending' => false, 
       'starting' => false, 
       'header' => true 
      )) 
      ->add('tab_1', new TabsType(), array(
       'ending' => false, 
       'label' => 'Podstawowe' 
      )) 
      ->add('firstName', null, array(
       'label' => 'Imię' 
      )) 
      ->add('lastName', null, array(
       'label' => 'Nazwisko' 
      )) 
      ->add('tab_contact', new TabsType(), array(
       'label' => 'Kontakt' 
      )) 
      ->add('address', new AddressType(), array(
       'label' => 'Adres zameldowania' 
      )) 
      ->add('tabs_end', new TabsType(), array(
       'starting' => false 
      )) 
    ; 
} 
1

Nếu bạn muốn có một hình thức để hành động như một thuật sĩ mẫu bạn có thể nhìn vào cái nhìn tại các multi-step form bundle

Nó khá đẹp, bạn có thể ví dụ, xác định bước một như điền vào các chi tiết phần mềm và sau đó trên bước 2, điền chi tiết phiên bản. hoặc bất cứ điều gì bạn muốn.

Tính năng

  • navigation (tiếp theo, trở lại, bắt đầu lại)
  • giới thiệu bước
  • bỏ qua các bước quy định
  • nhóm xác nhận khác nhau cho mỗi bước
  • bước năng động chuyển hướng

here là một bản demo sống

+0

tôi cần phải tải tất cả các trường mẫu cùng một lúc. – koral

0

Nhưng trong mẫu cành lá Tôi muốn sử dụng lệnh đơn

Bạn có nghĩa là để hiển thị các trường?

{{ form_rest(form) }} 

làm cho mọi hình thức unrendered

Các vấn đề liên quan