【问题标题】:Filters and validators are added twice过滤器和验证器被添加两次
【发布时间】:2013-10-03 10:49:41
【问题描述】:

我在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 验证器。这很烦人,尤其是当我使用 File\RenameUpload 过滤器并尝试重命名和移动已重命名的文件时。

【问题讨论】:

  • 找不到复制输入过滤器和验证器的位置,但尝试从 TestFieldset 中删除函数 getInputFilterSpecification() 并在其构造中执行此操作: $this->add(array( 'type' => 'text', 'name' => 'test', 'filters' => array(array('name' => 'string_trim'), ), 'validators' => array(array('name' => 'not_empty '), ), ));比检查它是否仍在重复。

标签: php zend-framework2 zend-form2


【解决方案1】:

此错误已在 zf 2.2.5 https://github.com/zendframework/zf2/issues/5270 中修复

【讨论】: