# Strange behavior with component Zend\\InputFilter and 'required' =\> false, validator NotEmpty and validator Regex

**URL:** https://discourse.laminas.dev/t/strange-behavior-with-component-zend-inputfilter-and-required-false-validator-notempty-and-validator-regex/581
**Category:** Components & MVC
**Tags:** zend-inputfilter
**Created:** [May 14, 2018, 2:58pm UTC](https://discourse.laminas.dev/t/strange-behavior-with-component-zend-inputfilter-and-required-false-validator-notempty-and-validator-regex/581 "2018-05-14T14:58:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![parideb](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/parideb/32/340_2.png) [@parideb](https://discourse.laminas.dev/u/parideb)
#### Post date: [May 14, 2018, 2:58pm UTC](https://discourse.laminas.dev/t/strange-behavior-with-component-zend-inputfilter-and-required-false-validator-notempty-and-validator-regex/581/1 "2018-05-14T14:58:21Z")

</div>

NotEmpty validator is not considered with the ‘required’ =\> false from the input filter validation.  
I would allow only to send a MD5 string, null have to be invalid. But the MD5 have not to be required.

This is the code to reproduce the strange behavior.

```auto
return new ValidationMiddleware([
   [
      'name' => 'name',
      'required' => false,
      'filters' => [
         [
            'name' => 'StripTags'
         ],
         [
            'name' => 'StringTrim'
         ],
         [
            'name' => 'StripNewlines'
         ]
      ],
      'validators' => [
         [
            'name' => 'NotEmpty',
         ],
         [
            'name' => 'Regex',
            'options' => [
               'pattern' => '/^[a-fA-F0-9]{32}$/'
         ]
      ]
   ]
]

```

When required attribute is set to false the validator NotEmpty it seems not to be considered. This means that I can send this request with this body

```auto
{
	"name": null
}

```

The request is valid also if the Regex is not valid: null should not be accepted by ‘/^[a-fA-F0-9]{32}$/’

If I remove ‘required’ =\> false from the input filter validation or I set to true, the filter work fine.  
The result is:

```auto
{
    "errors": [
        {
            "name": "name",
            "value": null,
            "messages": [
                "Value is required and can't be empty",
                "Invalid type given. String, integer or float expected"
            ]
        }
    ],
    "title": "Bad Request",
    "type": "https://httpstatus.es/400",
    "status": 400,
    "detail": "Validation error"
}

```

---

<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: [May 14, 2018, 5:48pm UTC](https://discourse.laminas.dev/t/strange-behavior-with-component-zend-inputfilter-and-required-false-validator-notempty-and-validator-regex/581/2 "2018-05-14T17:48:17Z")

</div>

Use the option `continue_if_empty`. Example:

```auto
$inputFilter = (new Zend\InputFilter\InputFilter())->getFactory()->createInputFilter(
    [
        [
            'name' => 'foo',
            'required' => false,
            'continue_if_empty' => true,
            'validators' => [
                [
                    'name' => Zend\Validator\NotEmpty::class,
                ],
                [
                    'name' => Zend\Validator\Regex::class,
                    'options' => [
                        'pattern' => '/^[a-fA-F0-9]{32}$/'
                    ]
                ]
            ]
        ]
    ]
);

$inputFilter->setData([]);
var_dump($inputFilter->isValid()); // true

$inputFilter->setData(['foo' => null]);
var_dump($inputFilter->isValid()); // false

$inputFilter->setData(['foo' => 'bar']);
var_dump($inputFilter->isValid()); // false

$inputFilter->setData(['foobar' => md5('bar')]);
var_dump($inputFilter->isValid()); // true

```

See also: [https://github.com/zendframework/zend-inputfilter/blob/master/src/Input.php#L417-L419](https://github.com/zendframework/zend-inputfilter/blob/master/src/Input.php#L417-L419)

---

<div class="post-metadata">

### Author: ![parideb](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/parideb/32/340_2.png) [@parideb](https://discourse.laminas.dev/u/parideb)
#### Post date: [May 16, 2018, 4:12pm UTC](https://discourse.laminas.dev/t/strange-behavior-with-component-zend-inputfilter-and-required-false-validator-notempty-and-validator-regex/581/3 "2018-05-16T16:12:22Z")

</div>

Thanks a lot, it works!
