Closed Maks3w closed 5 years ago
Is this fixed? Using the latest version, and it seems indeed that the collection input filter does not have some sort of default message when required is true and an empty array is given. Need to extend the input filter for now.
Issue is still exists. @weierophinney can you confirm this?
Any updates on this? Problem still exists.
Please provide us with a minimal coffee sample that demonstrates the issue; right now, we'd be doing guess work.
$factory = new \Zend\InputFilter\Factory();
$inputFilter = $factory->createInputFilter(
[
'collection-element' => [
'type' => \Zend\InputFilter\CollectionInputFilter::class,
'required' => true,
'input_filter' => [
'type' => [
'required' => true,
'validators' => [
[
'name' => \Zend\Validator\Between::class,
'options' => [
'min' => 50,
'max' => 100,
],
],
],
],
'price' => [
'required' => true,
'validators' => [
[
'name' => \Zend\Validator\Between::class,
'options' => [
'min' => 50,
'max' => 100,
],
],
],
],
],
],
]
);
$inputFilter->setData([]);
var_dump($inputFilter->isValid());
var_dump($inputFilter->getMessages());
$inputFilter->setData([
'collection-element' => [
0 => [
],
],
]);
var_dump($inputFilter->isValid());
var_dump($inputFilter->getMessages());
Output is:
boolean false
array (size=1)
'collection-element' =>
array (size=0)
empty
boolean false
array (size=1)
'collection-element' =>
array (size=1)
0 =>
array (size=2)
'type' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
'price' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
No error message is generated.
Compare the Input
class and the CollectionInputFilter
class in this case:
Do you have any updates related this issue?
@developer-devPHP No, no updates at this time.
We actually need to address this by getting some feedback first.
InputFilters do not have a concept of required/optional by default. We tacked it on to the CollectionInputFilter
(and, for the 2.8.0 release, the OptionalInputFilter
, which allows marking an input filter as optional, vs. required, meaning it can validate if the data set is empty). However, when we did, the only thing we had it affect is whether or not an empty set is valid. In the case that we have an empty set, there is currently no mechanism to provide a message why.
Normally, if data is present, getMessages()
will return a nested set of messages, one for each data set:
0 => [
'foo' => [ /* array of messages */]
],
1 => [
'bar' => [ /* array of mesages */ ]
],
/* etc. */
The problem area is that the each element of the above array is expected to be an array of key/messages pairs. How, exactly, do we represent a message indicating that a data set was empty in a way that is either consistent in structure, or easily differentiated from the existing structure?
Some thoughts off the top of my head:
__required__
:
[
0 => [
'__required__' => [ /* 'values are "required", but none present' messages */ ],
],
]
[
0 => 'Some error message here',
],
We need to choose one, but I need input from users as to which one to choose.
Additionally, we need:
If anybody would like to tackle this, I'd appreciate the help.
I have a solution for this. Please have a look into pull request #170
CollectionInputFilter fails without error message when is required but data is empty or not set.