3

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

例如:

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

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

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

4

4 回答 4

11

这在这个时候真的是不可能的。您最好的选择是使用回调过滤器并单独过滤每个项目。像这样的东西

$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;
    }))
  )
);
于 2013-08-17T23:46:39.877 回答
8

我意识到这很旧,但您可以将输入类型指定为ArrayInput并将InputFilter按预期处理它:

  "name" => "tags",
  "type" => "Zend\\InputFilter\\ArrayInput", // Treat this field as an array of inputs
  "filter" => array(
    array("name" => "StripTags"),
    array("name" => "StringTrim")
  )
于 2015-10-21T16:05:43.327 回答
4

我制作了一个 CollectionValidator,它将现有的验证器应用于数组中的所有项目。

我将它与 Apigility 一起使用:

'input_filter_specs' => [
    'Api\\Contact\\Validator' => [
        [
            'name'       => 'addresses',
            'required'   => false,
            'filters'    => [],
            'validators' => [
                [
                    'name'    => 'Application\\Validator\\CollectionValidator',
                    'options' => ['validator' => 'Api\\Address\\Validator']
                ]
            ],
            'description'=> 'List of addresses for contact'
        ],
        [
            'name'       => 'birthdate',
            # ...
        ]
    ],
]

我不确定这是否是您在控制器中使用验证器的方式,但可能是这样的:

new Collection(array('validator' => 'Zend\Validator\CreditCard'))

它返回validation_messages每个索引。假设它是创建联系人的 REST POST 请求,它表明第二个地址在邮政编码字段中包含错误。

{
  "detail": "Failed Validation",
  "status": 422,
  "title": "Unprocessable Entity",
  "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
  "validation_messages": {
    "addresses": {
      "1": {
        "zipcode": {
          "notAlnum": "The input contains characters which are non alphabetic and no digits"
        }
      }
    },
    "birthdate": {
      "dateInvalidDate": "The input does not appear to be a valid date"
    }
  }
}

集合验证器:

<?php
namespace Application\Validator;
class Collection extends \Zend\Validator\AbstractValidator  implements \Zend\ServiceManager\ServiceLocatorAwareInterface {
    protected $serviceLocator;
    protected $em;
    protected $messages;

    protected $options = array(
        'validator' => null
    );

    public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator->getServiceLocator();
    }

    public function getServiceLocator() {
        return $this->serviceLocator;
    }

    public function isValid($array) {
        $inputFilterManager = $this->getServiceLocator()->get('inputfiltermanager');
        $validatorName = $this->getOption('validator');

        $this->messages = [];
        $isvalid = true;
        foreach($array as $index => $item) {
            $inputFilter = $inputFilterManager->get($validatorName);
            $inputFilter->setData($item);
            $isvalid = $isvalid && $inputFilter->isValid($item);
            foreach($inputFilter->getMessages() as $field => $errors) {
                foreach($errors as $key => $string) {
                    $this->messages[$index][$field][$key] = $string;
                }
            }
        }
        return $isvalid;
    }

    public function getMessages() {
        return $this->messages;
    }
}

当前限制:

  • 不支持翻译
  • 仅返回第一个错误数组项的错误。
于 2014-10-09T12:39:00.090 回答
0

我有一个非常相似的问题,我能够用Zend\Form\Element\Collection.

使用集合元素,我能够验证看起来像的输入

$post = [
    [
        'idUser' => 1,
        'address' => 'foo street',
    ],
    [
        'idUser' => 2,
        'address' => 'bar street',
    ],
];

有关更详细的解释,请查看Zend 文档这个工作示例

于 2020-01-23T23:00:15.080 回答