Zf2如何使用过滤器类传递参数?

时间:2013-07-30 08:15:17

标签: php forms zend-framework filter zend-framework2


我想在zend framwork 2中使用SeparatorToSeparator()过滤器来过滤我的数据。
如何将两个参数(setSearchSeparator和setReplacementSeparator)传递给构造函数?

$inputFilter->add(array(
    'name' => 'supplierName',
    'required' => true,
    'filters' => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
        array('name'=>"Word\SeparatorToSeparator"
    ),
));

路径:zendframework / bin中/ libary / Zend的/过滤/字/ SeparatorToSeparator.php

class SeparatorToSeparator extends AbstractFilter
{
    protected $searchSeparator = null;
    protected $replacementSeparator = null;



       /**
         * Constructor
         *
         * @param  string  $searchSeparator      Separator to search for
         * @param  string  $replacementSeparator Separator to replace with
         */
        public function __construct($searchSeparator = ' ', $replacementSeparator = '-')
        {
            $this->setSearchSeparator($searchSeparator);
            $this->setReplacementSeparator($replacementSeparator);
        }

更新

$inputFilter->add(array(
            'name' => 'supplierName',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name'=>'Word\SeparatorToSeparator',
                      'options' => array(
                                    'search_separator'      => 'a',
                                    'replacement_separator' => 'b'
                                )
                     )
            ),
        ));

我收到此错误消息:

  

警告:preg_quote()期望参数1为字符串,数组为   C:\ WAMP \ WWW \ tebipdevelopment \供应商\ zendframework \ zendframework \库\ Zend的\过滤器\字\ SeparatorToSeparator.php   第92行

我打开了这一行,我打印了这样的错误信息。

print_r($this->searchSeparator);
print_r($this->replacementSeparator);

结果

数组([search_separator] => a [replacement_separator] => b)

在这种情况下,search_separator等于数组而不是字符串

2 个答案:

答案 0 :(得分:3)

请注意,您不需要setter,但我已经添加了它们,过滤器将尝试使用setter(如果它们存在)(setCamelCase()表示法。)

class SeparatorToSeparator extends AbstractFilter
{
    protected $searchSeparator = null;
    protected $replacementSeparator = null;

    public function setSearchSeparator($val)
    {
        $this->searchSeparator = $val;
    }

    public function setReplacementSeparator($val)
    {
        $this->replacementSeparator = $val;
    }
}

现在您可以设置选项:

$inputFilter->add(array(
    'name' => 'supplierName',
    'required' => true,
    'filters' => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
        array(
           'name'=>"Word\SeparatorToSeparator"
           'options' => array(
               'search_separator'      => 'bla',
               'replacement_separator' => 'bla'
           )
        )
    ),
));

答案 1 :(得分:1)

我遇到了同样的问题,与其他验证器不同,验证器一词不接受一系列选项,正如您所发现的那样。我使用的解决方法是首先实例化单词过滤器,将其传递给所需的构造函数参数,然后将该实例添加到过滤器规范......

$wordFilter = new \Zend\Filter\Word\SeparatorToSeparator('a', 'b');

$inputFilter->add(array(
    'name' => 'supplierName',
    'required' => true,
    'filters' => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
        $wordFilter,
    ),
));