Closed ghost closed 5 years ago
@kgrey
Use CollectionInputFilter (overkill?)
This is the correct solution for two reasons:
You can use:
$value = 'string_value';
$inputFilter->setData(
[
'array_of_values' => is_array($value) ? $value : [],
]
);
The problem with your current input-filter:
$inputFilter->setData(
[
'array_of_values' => [[[[]]]],
]
);
var_dump($inputFilter->isValid()); // true
$inputFilter->setData(
[
'array_of_values' => [
[
1,
2,
3,
],
[
[
new stdClass(),
]
],
'foobar'
],
]
);
var_dump($inputFilter->isValid()); // true
Please look also at the optional input-filter if you work with arrays of data: https://docs.zendframework.com/zend-inputfilter/optional-input-filters/
That's very useful. Thanks for the clarification. I'll give it a try.
This issue is more a query than a bug report, but I wasn't sure of the correct route to address the problem - whether that might be taking a different approach or in suggesting a fix.
One of the inputs on my input filter is using ArrayInput and is expecting an array of values. If I specify a string for the value in the data passed to setData() on the input filter then setValue() on the ArrayInput throws an exception because it's expecting an array.
Potentially the user might supply the request data incorrectly so it should cater for that scenario. Is it really necessary for me to pre-filter any array values in my own code before calling the input filter? That seems like something that should really be handled by the library; i.e. if a non-array value is passed to an array input, rather than throwing an exception, is recorded as an error against the input for retrieval via getMessages().
Other options:
Code to reproduce the issue
Input filter:
Call:
Expected results
Error reporting via the input filter itself.
Actual results
Zend\InputFilter\Exception\InvalidArgumentException Value must be an array, string given.