How to add 'sub elements' in Zend Form

67 views Asked by At

I have a Zend Form with several fields for the user to complete. In the admin system I want to display the exact same form but with additional Elements next to the existing ones where the administrator will be able to provide feedback to the user's input.

Below you can see the code I am using to create the form that the user sees.

Before posting this question I had a look on Zend Form Decorator but I did not understand if that's what I need to resolve this issue.

public function __construct()
    {
        parent::__construct('user-feedback-form');
        $this->setAttribute('method', 'post');
        $this->setAttribute('role', 'form');

        $this->add([
            'name' => 'name',
            'type' => Text::class,
            'attributes' => [
                'id' => 'name',
                'required' => true,
                'readonly' => false,
            ],
        ]);

        $this->add([
            'name' => 'surname',
            'type' => Text::class,
            'attributes' => [
                'id' => 'surname',
                'required' => true,
                'readonly' => false,
            ],
        ]);

        $this->add([
            'name' => 'age',
            'type' => Number::class,
            'attributes' => [
                'id' => 'age',
                'required' => true,
                'readonly' => false,
            ],
        ]);
    }
1

There are 1 answers

0
Tiv On

To reuse certain parts of a form zend provides the fieldsets. Instead of adding the elements to your form add them toa filedset and add the fieldset to the form.

class UserFeedbackFieldset extends Zend\Form\Fieldset 
{
    public function init()
    {
        $this->add([
            'name' => 'name',
            'type' => Text::class,
            'attributes' => [
                'id' => 'name',
                'required' => true,
                'readonly' => false,
            ],
        ]);

        $this->add([
            'name' => 'surname',
            'type' => Text::class,
            'attributes' => [
                'id' => 'surname',
                'required' => true,
                'readonly' => false,
            ],
        ]);

        $this->add([
            'name' => 'age',
            'type' => Number::class,
            'attributes' => [
                'id' => 'age',
                'required' => true,
                'readonly' => false,
            ],
        ]);
    }
}

Then in your forms add the fieldset:

class UserFeedbackForm extends Zend\Form\Form
{
    public function __construct()
    {
        parent::__construct('user-feedback-form');
        $this->setAttribute('method', 'post');
        $this->setAttribute('role', 'form');
    }

    public function init()
    {
        $this->add([
            'type' => UserFeedbackFieldset::class,
            'name' => 'user',
            'options' => [
                'use_as_base_fieldset' => true,
            ],
        ]);
    }
}

class AdminUserFeedbackForm extends Zend\Form\Form
{
    public function __construct()
    {
        parent::__construct('user-feedback-form');
        $this->setAttribute('method', 'post');
        $this->setAttribute('role', 'form');
    }

    public function init()
    {
        $this->add([
            'type' => UserFeedbackFieldset::class,
            'name' => 'user',
            'options' => [
                'use_as_base_fieldset' => true,
            ],
        ]);

        $this->add([
            'name' => 'someotherfield',
            'type' => Text::class,
            'attributes' => [
                'id' => 'someotherfield',
                'required' => true,
                'readonly' => false,
            ],
        ]);
    }
}

Then you can use the other form on your admin page instead of the original one.