How to validate a email list using Respect Validation

406 views Asked by At

I'm using Respect Validation classes and I want to validate a list of email separated by , or ; and spaces like:

[email protected]; [email protected],[email protected] ;[email protected];

I cannot use the standard email() rule and I did not find any rule for a mixed comma, semi-colon and space list.

The I tried to create a custom rule class and I put this inside my App\Validaton\Rules folder.

namespace App\Validation\Rules;

use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Rules\Email;

class Emails extends AbstractRule
{
    public function validate($input)
    {
        if (!is_string($input)) {
            return false;
        }

        $inputs=preg_split( "/;|,/", $input);

        $v=new Email();
        foreach($inputs as $input){
            if(!$v->validate($input))
                return false;
        }
        return true;
    }

}

How can I use my custom validator using static validator reference?

If I try this:

use \Respect\Validation\Validator as V
...
V::length(0,200)->emails()->validate($input);

I got:

"emails" is not a valid rule name

What I'm missing in the namespace inclusion?

1

There are 1 answers

0
Tobia On

I have solved by adding this line in the application bootstrap:

\Respect\Validation\Validator::with("\\App\\Validation\\Rules\\");