Zend Framework 2 过滤/验证内容数组 [英] Zend Framework 2 filter / validate array of contents

查看:23
本文介绍了Zend Framework 2 过滤/验证内容数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将过滤器应用于包含数组内容的字段元素?

How do I apply a filter to a field element with the contents of an array?

例如:

$this->add(
  "name" => "tags",
  "type" => "text",
  "filter" => array(
    array("name" => "StripTags"),
    array("name" => "StringTrim")
  )
);

$tags[0] = "PHP";
$tags[1] = "CSS";

如果我尝试过滤,我会收到一条错误消息,指出标量对象被排除在外,给出了数组.

If I attempt to filter I receive an error saying a scalar object is excepted, array given.

推荐答案

这在目前是不可能的.最好的办法是使用 回调过滤器 并单独过滤每个项目.像这样

This isn't really possible at this time. Your best bet is to use a Callback filter and filter each Item individually. Something like this

$this->add(
  "name" => "tags",
  "type" => "text",
  "filter" => array(
    array("name" => "Callback", "options" => array(
       "callback" => function($tags) {
          $strip = new \Zend\Filter\StripTags();
          $trim = new \Zend\Filter\StringTrim();
          foreach($tags as $key => $tag) {
            $tag = $strip->filter($tag);
            $tag = $trim->filter($tag);
            $tags[$key] = $tag;
          }
          return $tags;
    }))
  )
);

这篇关于Zend Framework 2 过滤/验证内容数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆