Whenever I need to use cpf
or telefone
validations in Laravel, I need to use the Validator::extend
method 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 it from my programming routine. Whenever I need this validation on some system made in Laravel, I always need to use CTRL + V in this code.
I think the solution to such a thing is if there were some library for Laravel, with some validations for Brazilian data ready (with error messages also ready).
Does anyone know of any library for Laravel 4 and Laravel 5, who does this?
I need it to be for both frameworks, because I work exactly with both, because I have several systems in the company where I work. It would be a "hand in the wheel" if someone helped me.