phpzend-framework3zend-inputfilterzend-form-fieldset

How to inherit getInputFilterSpecification method for nested fieldsets in ZF3


I am developing input filters in the fieldset classes for a ZF3 module using a class-table inheritance pattern. The ZF3 documentation says that the fieldset class must implement Zend\InputFilter\InputFilterProviderInterface, which defines a getInputFilterSpecification() method.

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

This works fine for independent fieldset classes, but if I have one fieldset extend another fieldset, the form uses the getInputFilterSpecification() method from the child only.

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

Since the getInputFilterSpecification() method is just a collection of return statements, I thought I could add a call to the parent method within the child method, but that doesn't seem to work:

// in child:

    public function getInputFilterSpecification()
    {

        parent::getInputFilterSpecification();

    // ... 

How can I get the getInputFilterSpecification() method in the child fieldset to inherit the code from the getInputFilterSpecification() method from the parent?


Solution

  • Thanks to Dolly Aswin's comment, here is the answer:

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