如何继承 ZF3 中嵌套字段集的 getInputFilterSpecification 方法
How to inherit getInputFilterSpecification method for nested fieldsets in ZF3
我正在使用 class-table 继承模式在字段集 classes 中为 ZF3 模块开发输入过滤器。 ZF3 documentation 表示字段集 class 必须实现 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(),
],
],
];
}
}
这适用于独立字段集 classes,但如果我有一个字段集扩展另一个字段集,该表单仅使用子项的 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()
方法的代码?
感谢 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(),
],
],
];
}
}
我正在使用 class-table 继承模式在字段集 classes 中为 ZF3 模块开发输入过滤器。 ZF3 documentation 表示字段集 class 必须实现 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(),
],
],
];
}
}
这适用于独立字段集 classes,但如果我有一个字段集扩展另一个字段集,该表单仅使用子项的 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()
方法的代码?
感谢 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(),
],
],
];
}
}