How do I validate boolean?

In a form there are boolean fields my question is how do we validate boolean values or we can safely ignore validation for boolean. The same question arises for form elements - Select, Multi Select fields.

There is a Boolean input filter class.

<?php
declare(strict_types=1);
namespace Marcel\InputFilter;

use Laminas\Filter\Boolean;
use Laminas\Filter\ToNull;
use Laminas\InputFilter\InputFilter;
use Laminas\Validator\Callback;

class ExampleInputFilter extends InputFilter
{
    public function init()
    {
        $this->add([
            'name' => 'your-input-field',
            'required' => true,
            'allow_empty' => false,
            'filters' => [
                 [ 'name' => Boolean::class ],
                 [
                    'name' => ToNull::class,
                    'options' => [
                        'type' => ToNull::TYPE_BOOLEAN,
                    ],
                ],
            ],
            'validators' => [
                [
                    'name' => Callback::class,
                    'options' => [
                        'callback' => function($value) {
                                // value can be true or false or null
                        }
                    ],
                ]
            ],
        ]);
    }
}

The code shown above filters the input field your-input-field to a boolean value. The boolean filter casts booleans like 1/0 or true/false (as strings) to real boolean values. You can affect the boolean filter by setting according options. Me personally combines the boolean filter with the to null filter, so that empty values cast to null.

Additionally you can validate the filtered value with a callback validator. Just write a callback, which does what you want.

Generally, select boxes also need to be validated, since the user can change the values sent from a select box in a form.

Nothing that comes from a browser should be trusted in any way since users can manipulate their requests.

In the example above I think the filtered value could only be Boolean and not Null. I just want to test my understanding(that could be wrong as well). Because the data is first filtered for Boolean Filter and then Null Filter. So in my opinion Because of filter chaining you only get TRUE or FALSE in first filter and there is no Null left in the data.

You 're right. The default behaviour of the Boolean filter is casting empty values to boolean false. In this case the Null filter just returns false because it checks, if the given value is bool. The Null filter would work under certain options for the Boolean filter. Forget that step, because it 's just an additional individual step.

I wonder what is the benefit of having a Validator for Boolean - the value after it is filtered is either True or False. Or in a validator we just check it is in fact either of these 2 values(true/false)?

I myself no longer validate for boolean values at all if they have already been filtered. From my point of view, a validator only makes sense if you want to check further information in the context of this boolean value.

Imagine a question that you could only answer yes or no to. If you answer yes to the question, further questions must be answered. These further questions could be validated depending on the answer to the yes/no question. The yes/no value itself, however, does not need to be validated further, as it is reduced to the values true or false by the Boolean filter itself. Therefore, you are correct in your assumption that, as long as you use the Boolean filter, the value itself does not need to be further validated. Unless you have some concerns of your own.

I am using the following filter but still empty data (‘’) passes through. My expectation is filter will convert ‘’ to false:

 // Add input for "my-dsc-selected" field
        $inputFilter->add([
            'name'     => 'my-dsc-selected',
            'filters'  => [
                //This filter changes a given input to be a BOOLEAN value. 
                [  
                    'name' => Boolean::class,
                    'options' => [
                        'type' => [
                            'integer',
                            'zero',
                            'string',
                        ],
                    ],
                    
                ]
            ],  
            
            
        ]);

Any pointers please.