Developing software in the Real World

Zend\Input and empty values

I'm forever getting confused about how the combination of Zend\Input's required, allow_empty & continue_if_empty interact with an empty value, so I've decided to write it down.

These settings define what happens when you try to validate an empty value for a given input. For Zend\Input, empty means exactly equal to null, an empty string or an empty array.

Firstly, let's start with the three settings:

Setting Default What it does
required true When true, the value must not be empty (unless allow_empty is true).
allow_empty false When true, the field may be empty if it's required.
continue_if_empty false When true, the validators are run even if the field is empty.

Firstly, contine_if_empty controls if the validators are run for an empty value. If contine_if_empty is true, then the validators are always executed.

If contine_if_empty is false, then the decision on whether to run the validators depends on the combination of required and allow_empty:

required allow_empty What happens
true false For an empty value, the validators are run. isValid() is determined by the validators and is false as the NotEmpty validator fails. (This is the default)
true true For an empty value, the validators are not run. isValid() = true.
false false For an empty value, the validators are not run. isValid() = true.
false true For an empty value, the validators are not run. isValid() = true.

Test app

To prove this, I wrote this little test app:

You'll need to run composer require zendframework/zend-inputfilter if you want to test it yourself.

Interesting side note: I got to use the splat operator, so this is PHP 5.6 only code :)

The test is quite simple. We add a StringLength validator to an input so that we know that the validators have run as it will always fail. We then iterate through all the the combinations of required, allow_empty & continue_if_empty and see what happens.

The output is:

Conclusion

The interactions between required, allow_empty & continue_if_empty aren't that complicated, but there's enough complexity that it's worth having a reference handy!

3 thoughts on “Zend\Input and empty values

Comments are closed.