Как наследовать метод 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() метод от родителя?

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(),
                ],
            ],
        ];
    }
}
Другие вопросы по тегам