Как наследовать метод getInputFilterSpecification для вложенных наборов полей в ZF3

Я разрабатываю входные фильтры в классах набора полей для модуля ZF3, используя шаблон наследования таблицы классов. В документации ZF3 говорится, что класс fieldset должен реализовать Zend\InputFilter\InputFilterProviderInterface, который определяет метод getInputFilterSpecification().

namespace Contact\Form;

use Zend\Filter;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Validator;

class SenderFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function getInputFilterSpecification()
    {
        return [
            'name' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    [
                        'name' => Validator\StringLength::class,
                        'options' => [
                            'min' => 3,
                            'max' => 256
                        ],
                    ],
                ],
            ],
            'email' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    new Validator\EmailAddress(),
                ],
            ],
        ];
    }
}

Это прекрасно работает для независимых классов наборов полей, но если у меня есть один набор полей, расширяющий другой набор полей, форма использует метод getInputFilterSpecification() только из дочернего элемента.

namespace Contact\Form;

use Zend\Filter;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Validator;

class PersonFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function getInputFilterSpecification()
    {
        return [
            'name' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    [
                        'name' => Validator\StringLength::class,
                        'options' => [
                            'min' => 3,
                            'max' => 256
                        ],
                    ],
                ],
            ],
        ];
    }
}

class SenderFieldset extends PersonFieldset implements InputFilterProviderInterface
{
    public function getInputFilterSpecification()
    {
        return [
            'email' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    new Validator\EmailAddress(),
                ],
            ],
        ];
    }
}

Поскольку метод getInputFilterSpecification() представляет собой просто набор операторов возврата, я подумал, что могу добавить вызов родительского метода в дочерний метод, но это, похоже, не работает:

// in child:

    public function getInputFilterSpecification()
    {

        parent::getInputFilterSpecification();

    // ... 

Как я могу получить метод getInputFilterSpecification() в дочернем наборе полей, чтобы наследовать код от метода getInputFilterSpecification() от родителя?


person jcropp    schedule 16.02.2018    source источник
comment
Я думаю, вам просто нужно добавить return к этому методу return parent::getInputFilterSpecification();   -  person Dolly Aswin    schedule 16.02.2018
comment
отсутствует return проблема?   -  person Dolly Aswin    schedule 16.02.2018
comment
return parent::getInputFilterSpecification(); сделал свое дело,   -  person jcropp    schedule 16.02.2018


Ответы (1)


Благодаря комментарию Долли Асвин, вот ответ:

namespace Contact\Form;

use Zend\Filter;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Validator;

class PersonFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function getInputFilterSpecification()
    {
        return [
            'name' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    [
                        'name' => Validator\StringLength::class,
                        'options' => [
                            'min' => 3,
                            'max' => 256
                        ],
                    ],
                ],
            ],
        ];
    }
}

class SenderFieldset extends PersonFieldset implements InputFilterProviderInterface
{
    public function getInputFilterSpecification()
    {

        // get input filter specifications from parent class
        return parent::getInputFilterSpecification();

        return [
            'email' => [
                'required' => true,
                'filters'  => [
                    ['name' => Filter\StringTrim::class],
                ],
                'validators' => [
                    new Validator\EmailAddress(),
                ],
            ],
        ];
    }
}
person jcropp    schedule 16.02.2018