我正在使用类表继承模式为ZF3模块开发字段集类中的输入过滤器。 ZF3 documentation表示字段集类必须实现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()
方法只是一个return语句的集合,我想我可以在子方法中添加对父方法的调用,但这似乎不起作用:
// in child:
public function getInputFilterSpecification()
{
parent::getInputFilterSpecification();
// ...
如何从子字段集中获取getInputFilterSpecification()
方法以从父级继承getInputFilterSpecification()
方法中的代码?
答案 0 :(得分: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(),
],
],
];
}
}