ircmaxell / filterus

A simple filtering library for PHP
454 stars 55 forks source link

Email validation doesn't work on an empty string #14

Open halfer opened 6 years ago

halfer commented 6 years ago

There isn't an implementation for validate for emails, so although an empty string will be correctly filtered to null, it will validate as true, which is not desirable.

Repro code:

$filter = \Filterus\Filter::factory('email');

$result = $filter->filter('');
var_dump($result); // Returns NULL, correct

$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Incorrectly returns Passed

A solution for now is to use a chain, like so:

$filter = \Filterus\Filter::chain('email', 'string,min:1');

$result = $filter->filter('');
var_dump($result); // Returns NULL, correct

$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Correctly returns Failed