How do I apply a filter to a field element with the contents of an array?
For example:
$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
I had a very simular issue and I was able to solve it with
Zend\Form\Element\Collection
.With the Collection Element I was able to validate inputs that looks like
For a more detailed explanation check out the Zend Documentation and this working example
I realize this is old but you can specify the input type as
ArrayInput
andInputFilter
will handle it as expected:I've made a CollectionValidator that applies an existing validator to all items in an array.
I'm using it with Apigility as such:
I'm not sure if this is how you would use a validator inside a controller, but probably something like this:
It returns
validation_messages
per index. Let's say it was REST POST request to create a contact, it indicates that the second address contains an error in the zipcode field.The Collection validator:
Current limitations: