0

我正在使用类表继承模式为 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()

4

1 回答 1

0

感谢 Dolly Aswin 的评论,答案如下:

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(),
                ],
            ],
        ];
    }
}
于 2018-02-16T18:38:32.557 回答