Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
halfer opened this issue Mar 6, 2018 · 0 comments
Open

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

halfer opened this issue Mar 6, 2018 · 0 comments

Comments

@halfer
Copy link

halfer commented Mar 6, 2018

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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant