Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@halfer

Description

@halfer

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');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions