Library for data validations in Brazilian Portuguese for Laravel

8

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.

    
asked by anonymous 27.05.2016 / 18:10

1 answer

6

Yes. You have that you can use.

To install via composer in Laravel 4

{
    "phplegends/pt-br-validator" : "1.*"
}

Already at laravel 5

{
    "phplegends/pt-br-validator" : "2.*"
}

In%% of you need to add the Library Service Provider.

Laravel 5:

PHPLegends\PtBrValidator\ValidatorProvider::class

Laravel 4:

 'PHPLegends\PtBrValidator\ValidatorProvider'

github example:

$validator = Validator::make(
    ['telefone' => '(77)9999-3333'],
    ['telefone' => 'required|telefone_com_ddd']
);

dd($validator->fails());


Validator::make($valor, $regras, ['celular_com_ddd' => 'O campo :attribute não é um celular'])
    
27.05.2016 / 18:37