0

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

...

Vaishnavesh
  • 154
  • 1
  • 2
  • 13
  • Possible duplicate of [Zend Framework 3 - Add and Remove new input element section using javascript](https://stackoverflow.com/questions/52490466/zend-framework-3-add-and-remove-new-input-element-section-using-javascript) - Flagged it as a duplicate. Complete example can be found in [this answer](https://stackoverflow.com/questions/52490466/zend-framework-3-add-and-remove-new-input-element-section-using-javascript/52492527#52492527). [Additional info](https://stackoverflow.com/questions/53299020/zend-servicemanager-using-setter-injection/53315617#53315617). (sorry, been asked a lot lately) – rkeet Dec 03 '18 at 21:58
  • As an even greater help, you might want to have a look at [this module](https://github.com/rkeet/zf-doctrine-form) I created to help out with creating forms. Though example module ([here](https://github.com/rkeet/zf-doctrine-form-examples)) uses Doctrine, it's easily modified for the Reflection Hydrator. – rkeet Dec 03 '18 at 22:03
  • @rkeet I went through the link but it is different from my question. Just wanted the same fieldset in nested(n number depth). – Vaishnavesh Dec 04 '18 at 11:27
  • This is precisely what my module enables and you can learn from reading through that code ;-) – rkeet Dec 04 '18 at 13:29

0 Answers0