如何在嵌套集合中使用一个表单字段集?

时间:2018-12-03 10:51:50

标签: zend-framework2 zend-form zend-framework3 zend-form-fieldset zend-form-collection

我正在创建页面以插入书籍之类的数据。我在哪里使用zend表单字段集和集合。字段集只有两个字段标题字段和内容字段。标题和子标题具有相同的内容(字段)。

例如:

1: Heading Content
   1.1: Sub Heading
   Content
      1.1.1: Sub Heading
      Content
      1.1.2: Sub Heading
      Content
         1.1.2.1: Sub Heading
         Content
         1.1.2.2: Sub Heading
         Content
         ...
      1.1.3: Sub Heading
      Content
      ..
   1.2: Sub Heading
   Content 
   ...
2: Heading Content

注意:这里的索引只是为了显示父子关系。

代码如下:

这是主要形式。

class BookPointlForm extends Form
 {
     public function __construct($name = null)
     {
         parent::__construct('Form');
         $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'points',
            'attributes' => array(
               'class'=> 'point_collection '
             ),
            'options' => array(
                'label' => 'Add Book Points',
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'Book\Form\BookPointMainFieldset',
                ),
            ),
        ));

         $this->add(array(
             'name' => 'submit',
             'type' => 'Submit',
             'attributes' => array(
                 'value' => 'Submit',
                 'id' => 'submitbutton',
                 'class' => 'btn btn-primary pull-right',
             ),
         ));
     }
 }

在BookPointlForm的点集合中调用此字段集

class BookPointMainFieldset extends Fieldset implements InputFilterProviderInterface
 {
     public function __construct($name = null)
     {
         // we want to ignore the name passed
         parent::__construct('Fieldset');

         $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'points',
            'attributes' => array(
               'class'=> 'point_collection '
             ),
            'options' => array(
                'label' => 'Add Book Points',
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'Book\Form\BookPointFieldset',
                ),
            ),
        ));

         $this->add(array(
             'name' => 'add_nested_point',
             'type' => 'Button',
             'attributes' => array(
                 'value' => 'Add nested point',
                 'class'=> 'add_nested_point'
             ),
             'options' => array(
                'label' => 'Add Nested Point',
                 ),
         ));
         $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'nested-points',
            'attributes' => array(
               'class'=> 'nested_point_collection '
             ),
            'options' => array(
                'label' => 'Add Book Points',
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'Book\Form\BookPointFieldset',
                ),
            ),
        ));

     }
     public function exchangeArray($data) {

    }
     public function getInputFilterSpecification()
     {
         return array(
             'weight' => array(
                 'required' => true,
             ),
         );
     }
 }

这是主要字段集,其中包含标题和标题内容

class BookHeadingFieldset extends Fieldset implements InputFilterProviderInterface
 {
     public function __construct($name = null)
     {
         $kraHeadingText = '';
         // we want to ignore the name passed
         parent::__construct('BookHeadingFieldset');

//         $this
//             ->setHydrator(new ClassMethodsHydrator(false))
//             ->setObject(new KraHeading())
         ;
         $this->add(array(
             'name' => 'id',
             'type' => 'Hidden',
             'attributes' => array(
                    'value' => ''
            )
         ));
         $this->add(array(
             'name' => 'manualHeadingText',
             'type' => 'Textarea',
             'options' => array(
                 'label' => 'Heading',
                 'label_options' => [
                     'disable_html_escape' => true
                     ],

             ),
             'labelOptions' => array(
                 'disable_html_escape' => true,
             ),
             'attributes' => array(
                 'class' => 'form-control',
                 'placeholder'=>"Enter heading",
                 'COLS'=>"150",
//                'required' => 'true',
             ),
         ));



     }
     public function exchangeArray($data) {
    }
     public function getInputFilterSpecification()
     {
         return array();
     }
 }

...

0 个答案:

没有答案
相关问题