[英]How to inherit getInputFilterSpecification method for nested fieldsets in 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()
方法只是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(),
],
],
];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.