Hi All
I have been having issues dealing with arrays and the InputFilter’s “required” and “allow_empty” options.
It is my understanding that:
- required = The input array must contain the field name as a key
- allow_empty = The value for the input can be empty (i.e. empty string, empty array, etc.)
With a standard “Input” type, everything works as expected:
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'textField',
'type' => Input::class,
'required' => true,
'allow_empty' => true,
'continue_if_empty' => false,
'filters' => [
['name' => StripTags::class],
]
]);
$inputFilter->setData([
'textField' => ''
]);
$result = $inputFilter->isValid(); // true
However, when using the ArrayInput type, an empty array is flagged as invalid:
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'arrayField',
'type' => ArrayInput::class,
'required' => true,
'allow_empty' => true,
'continue_if_empty' => false,
'filters' => [
['name' => ToInt::class],
]
]);
$inputFilter->setData([
'arrayField' => []
]);
$result = $inputFilter->isValid(); // false
$inputFilter->getMessages(); // [[isEmpty] => Value is required and can't be empty]
Am I misunderstanding how these options are intended to work, or is this possibly a bug?