# Why does InputFilter allow\_empty + required with ArrayInput differ to regular Inputs?

**URL:** https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809
**Category:** Components & MVC
**Tags:** laminas-inputfilter
**Created:** [December 6, 2024, 4:42pm UTC](https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809 "2024-12-06T16:42:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GLuskin](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/gluskin/32/1770_2.png) [@GLuskin](https://discourse.laminas.dev/u/GLuskin)
#### Post date: [December 6, 2024, 4:42pm UTC](https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809/1 "2024-12-06T16:42:07Z")

</div>

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:

```auto
$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:

```auto
$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?

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [December 6, 2024, 8:46pm UTC](https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809/2 "2024-12-06T20:46:45Z")

</div>

Hello and welcome to our forums! 😃

> [@GLuskin](#):
>
> However, when using the ArrayInput type, an empty array is flagged as invalid:

It corresponds to the tests:

> <https://github.com/laminas/laminas-inputfilter/blob/214b033769616f4211ffb63b432b716d786622c9/test/ArrayInputTest.php#L41-L49>

> <https://github.com/laminas/laminas-inputfilter/blob/214b033769616f4211ffb63b432b716d786622c9/src/ArrayInput.php#L87-L92>

But feel free to [open an issue report](https://github.com/laminas/laminas-inputfilter/issues) if you think it should be different.

* * *

And please compare with:

```php
$inputFilter->setData([
    'arrayField' => ['']
]);

```

```php
$inputFilter->setData([
    'arrayField' => [null]
]);

```

---

<div class="post-metadata">

### Author: ![GLuskin](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/gluskin/32/1770_2.png) [@GLuskin](https://discourse.laminas.dev/u/GLuskin)
#### Post date: [December 7, 2024, 6:46pm UTC](https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809/3 "2024-12-07T18:46:26Z")

</div>

> [@froschdesign](#):
>
> And please compare with:
> 
> ```auto
> $inputFilter->setData([
> 'arrayField' => ['']
> ]);
> 
> ```
> 
> ```auto
> $inputFilter->setData([
> 'arrayField' => [null]
> ]);
> 
> ```

Now I understand (I think)…

When using an ArrayInput, the “allow\_empty” option does not mean “the array can be empty”, instead, it means “the elements of the array can be empty”.

Is this the intended behavior? I didn’t find any tests (or docs) that cover the “allow\_empty” option with the ArrayInput

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [December 10, 2024, 8:43am UTC](https://discourse.laminas.dev/t/why-does-inputfilter-allow-empty-required-with-arrayinput-differ-to-regular-inputs/3809/4 "2024-12-10T08:43:20Z")

</div>

This will be reworked and clarified with the next major update of laminas-inputfilter (v3) because this has not been further documented so far.
