laminas-api-tools/api-tools-content-validation

Problem with required field, when type is set

weierophinney opened this issue · 3 comments

Hello!

Found such a case - if field is not set to required, but value is set to false:

[ 'required' => false, 'validators' => [], 'filters' => [], 'name' => 'debug', 'field_type' => 'boolean', ]

On request like this:

{"debug":false}

It will give an error:

{"validation_messages":{"debug":{"isEmpty":"Value is required and can't be empty"},"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Unprocessable Entity","status":422,"detail":"Failed Validation"}


Originally posted by @tasselchof at zfcampus/zf-content-validation#90

Can you show the full input-filter configuration? I am almost certain the problem lies somewhere else. If this is really a bug then it should be reported at the correct repository: zendframework/zend-inputfilter.


Originally posted by @Wilt at zfcampus/zf-content-validation#90 (comment)

0 => [
                'required' => false,
                'validators' => [],
                'filters' => [],
                'name' => 'debug',
                'description' => 'Output debug information.',
            ],

Here it is.


Originally posted by @tasselchof at zfcampus/zf-content-validation#90 (comment)

I have the exact same problem with an identical setup. If you pass boolean true then it's fine. If you don't set the element at all, it's fine (not required after all). But boolean false always triggers {"isEmpty":"Value is required and can't be empty"}. Seems the easiest way around this is to add 'allow_empty' => true to your configuration and condition your data accordingly at the other end. Probably just assume if it aint a boolean true then it's false.


Originally posted by @Bombdog at zfcampus/zf-content-validation#90 (comment)