Library for data validations in Brazilian Portuguese for Laravel

Asked

Viewed 152 times

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

  • @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)

  • You already tried using this guy: https://github.com/KennedyTedesco/Validation ?

  • It seems much more complete

  • Options are always venvidas @gmsantos. I didn’t know, I’ll take a look

  • @gmsantos vi here, it’s really quite complete. It’s worth taking a look. Also, right, it could only be from Respect

Show 1 more comment

1 answer

6


Yes. Has That one that you can use.

To install via Poser in Laravel 4

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

already in Laravel 5

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

In the app.php you need to add the library’s Service Provider.

Laravel 5:

PHPLegends\PtBrValidator\ValidatorProvider::class

Laravel 4:

 'PHPLegends\PtBrValidator\ValidatorProvider'

Example of github:

$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'])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.