Как добавить «субэлементы» в Zend Form - PullRequest
0 голосов
/ 31 января 2019

У меня есть Zend Form с несколькими полями, которые пользователь может заполнить.В системе администратора я хочу отобразить точно такую ​​же форму, но с дополнительными элементами рядом с существующими, где администратор сможет предоставить обратную связь для ввода пользователя.

Ниже вы можете увидеть код, который я используючтобы создать форму, которую видит пользователь.

Перед публикацией этого вопроса я заглянул в Zend Form Decorator, но не понял, нужно ли мне решить эту проблему.

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 Ответ

0 голосов
/ 15 февраля 2019

Для повторного использования определенных частей формы Zend предоставляет наборы полей.Вместо добавления элементов в форму добавьте их в набор файлов и добавьте набор полей в форму.

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

Затем в свои формы добавьте набор полей:

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

Затем вы можете использоватьдругая форма на вашей странице администратора вместо оригинальной.

...