8
Whenever I need to use cpf
or telefone
in Laravel, I need to use the method Validator::extend
to add these validations.
Validator::extend('cpf_real', function($attr, $value)
{
$c = preg_replace('/\D/', '', $value);
if (strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
return false;
}
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
return true;
});
I noticed that this has become a repetitive process and wanted to remove this from my programming routine. Every time I need this validation in some system done in Laravel, I always need to use CTRL+V in this code.
I believe that the solution to such a thing is if there were some library for Laravel, with some validations for Brazilian data ready (with the error messages also ready).
Does anyone know any library for Laravel 4 and Laravel 5 that does this?
I need it to be for both frameworks, because I work with them exactly, because I have several systems in the company where I work. It would be a "hand on the wheel" if someone would help me.
I’ve heard of Phplegends. Do you know? rs , https://github.com/phplegends, Ps: check the Phplegends developers
– Miguel Batista
@João is, but any project I had to start, I would have to create a new Serviceprovider. And that’s because you’re talking about Laravel 5. Because I also work with 4 (as I said in the question)
– Wallace Maxters
You already tried using this guy: https://github.com/KennedyTedesco/Validation ?
– gmsantos
It seems much more complete
– gmsantos
Options are always venvidas @gmsantos. I didn’t know, I’ll take a look
– Wallace Maxters
@gmsantos vi here, it’s really quite complete. It’s worth taking a look. Also, right, it could only be from Respect
– Wallace Maxters