Developing software in the Real World

Setting up required fields that can be empty with Zend\InputFilter

When you create an input filter entry that has the required element set to true, then by default, the field is set up so that it must have a value in it. If you want the field to be required, but also an empty value is okay, then add the allow_empty element to your definition:

In this definition, we have a ‘notes’ element that must exist, but can be an empty string.

That’s all there is to it.

5 thoughts on “Setting up required fields that can be empty with Zend\InputFilter

    1. We've had cases before where we've had a select box with an option in it that had a blank value. We wanted the user to choose an option from the select box, so wanted it to be required.

  1. @Mike . That means that if you bind the data to the form that has a field with required and allowempty set to true, that field should receive at least receive the array key from binded data even if it doesn't have a value. That is important if you are binding from post or get request

    ex. in your controller:
    $form->setData($this->getRequest()->getPost());

    A techy user would delete a textbox from your form using firebug, then it wouldn't receive your required field key.

  2. You can use it for form protection from bots. We can include input field, this field should be empty.
    For example:
    we hide style into css file.
    "login" name is a trap for bots.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *