5
我寫了關於它的zf2 github沒有人解答。但也許這不是一個錯誤,我做錯了什麼。這裏是我的代碼:過濾器和校驗添加兩次
字段集
class TestFieldset extends Fieldset implements
InputFilterProviderInterface
{
public function __construct($name)
{
parent::__construct($name);
$this->add(array(
'type' => 'text',
'name' => 'test'
));
}
public function getInputFilterSpecification() {
return array(
'test' => array(
'filters' => array(
array('name' => 'StringTrim')
),
'validators' => array(
array('name' => 'NotEmpty')
)
)
);
}
}
形式
class TestForm extends Form
{
public function __construct($name = null, $options = array()) {
parent::__construct($name, $options);
$fieldset = new TestFieldset('test-fieldset');
$this->add($fieldset);
}
}
控制器動作
public function indexAction()
{
$form = new \CRM\Form\TestForm;
$form->setData(array('test-fieldset' => array('test' => 'test value')));
$form->isValid();
$inputFilter = $form->getInputFilter()
->get('test-fieldset')
->get('test');
$filters = $inputFilter->getFilterChain()
->getFilters();
$validators = $inputFilter->getValidatorChain()
->getValidators();
var_dump($filters);
var_dump($validators);
}
結果
object(Zend\Stdlib\PriorityQueue)[678]
protected 'queueClass' => string 'Zend\Stdlib\SplPriorityQueue' (length=28)
protected 'items' =>
array (size=2)
0 =>
array (size=2)
'data' =>
object(Zend\Filter\StringTrim)[682]
...
'priority' => int 1000
1 =>
array (size=2)
'data' =>
object(Zend\Filter\StringTrim)[693]
...
'priority' => int 1000
protected 'queue' =>
object(Zend\Stdlib\SplPriorityQueue)[683]
protected 'serial' => int 2147483645
array (size=2)
0 =>
array (size=2)
'instance' =>
object(Zend\Validator\NotEmpty)[686]
protected 'constants' =>
array (size=13)
...
protected 'messageTemplates' =>
array (size=2)
...
protected 'options' =>
array (size=1)
...
protected 'value' => string 'test value' (length=10)
protected 'abstractOptions' =>
array (size=7)
...
'breakChainOnFailure' => boolean false
1 =>
array (size=2)
'instance' =>
object(Zend\Validator\NotEmpty)[697]
protected 'constants' =>
array (size=13)
...
protected 'messageTemplates' =>
array (size=2)
...
protected 'options' =>
array (size=1)
...
protected 'value' => string 'test value' (length=10)
protected 'abstractOptions' =>
array (size=7)
...
'breakChainOnFailure' => boolean false
正如你可以看到,有兩個StringTrim過濾器和兩個NotEmpty驗證。這很煩人,尤其是當我使用的文件\ RenameUpload過濾器和它試圖重新命名和移動已重命名的文件。
無法找到它的重複輸入過濾和驗證,但嘗試刪除功能'getInputFilterSpecification()'從TestFieldset併爲此在它的結構: $這 - >加(陣列( '類型'=> '文本', '名稱'=> '測試', '過濾器'=>數組( 陣列( '姓名'=> 'string_trim'), ) , '驗證器'=>數組( 陣列( '姓名'=> 'not_empty'), ) )); 比檢查它是否仍然重複。 –