filterus icon indicating copy to clipboard operation
filterus copied to clipboard

What's the best way to allow strings to be null?

Open halfer opened this issue 7 years ago • 0 comments

Hi, thanks for providing this library. I have the following solution to allow "string or null", and will provide it below. However, if there is a better way to do this, please let me know.

First a null validator:

/**
 * Class to allow nulls
 */

class AllowNull extends \Filterus\Filter
{
    public function filter($var)
    {
        return $var;
    }

    public function validate($var)
    {
        return is_null($var);
    }
}

Then register like so:

use Filterus\Filter as F;

F::registerFilter('null', AllowNull::class);

Finally use in a pool:

$validator = F::pool('string', 'null');

halfer avatar Mar 06 '18 14:03 halfer