How can I use one form fieldset in nested collection?

194 Views Asked by At

I am creating page to insert data like a book. Where I'm using zend form fieldset and collection. Fieldset have only two fields heading field and content field. Heading and sub-heading have same content(fields).

Ex:

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

Note: here indexing is just to show the parent child relationship.

Code are below:

This is main form.

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',
             ),
         ));
     }
 }

This fieldset is called in BookPointlForm's points collection

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,
             ),
         );
     }
 }

This is main fieldset it contents heading and heading's content

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

There are 0 best solutions below