laravellegends/pt-br-validator

一个包含巴西格式验证的库,适用于Laravel

11.0.0 2024-03-18 20:18 UTC

README

此库为Laravel添加了巴西验证,如CPF、CNPJ、车牌、CEP、电话、手机等。

🇧🇷🇧🇷🇧🇷

版本

安装

导航到您的项目文件夹,例如

cd /etc/www/projeto

然后执行

composer require laravellegends/pt-br-validator

如果您使用的是此库的5.2之前的版本,您必须在config/app.php中提供provider

'providers' => [
    // ... outros pacotes
    LaravelLegends\PtBrValidator\ValidatorProvider::class
]

现在,要使用验证,只需按照Laravel的标准程序。

区别在于可以使用以下验证方法

测试PtBrValidator的验证

这样,可以进行简单的测试

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

dd($validator->fails());

您也可以通过Illuminate\Http\Request的实例使用它,通过validate方法。

查看

use Illuminate\Http\Request;

// URL: /testando?telefone=3455-1222

Route::get('testando', function (Request $request) {

    try{

        $dados = $request->validate([
            'telefone' => 'required|telefone',
            // outras validações aqui
        ]);

    } catch (\Illuminate\Validation\ValidationException $e) {
        dd($e->errors());
    }

});

自定义消息

上述所有验证都已包含默认验证消息,但您可以使用Validator::make的第三个参数来更改此设置。该参数应是一个数组,其中索引是验证名称,值是相应的消息。

例如

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

或者通过您的php artisan make:request创建的Request实例的messages方法。

public function messages() {

    return [
        'campo.telefone' => 'Telefone não válido!'
    ];
}

单独访问规则

如果您需要单独访问某个规则,您可以访问以下类

\LaravelLegends\PtBrValidator\Rules\Celular::class
\LaravelLegends\PtBrValidator\Rules\CelularComDdd::class
\LaravelLegends\PtBrValidator\Rules\CelularComCodigo::class
\LaravelLegends\PtBrValidator\Rules\Cnh::class
\LaravelLegends\PtBrValidator\Rules\Cnpj::class
\LaravelLegends\PtBrValidator\Rules\Cpf::class
\LaravelLegends\PtBrValidator\Rules\Cns::class
\LaravelLegends\PtBrValidator\Rules\FormatoCnpj::class
\LaravelLegends\PtBrValidator\Rules\FormatoCpf::class
\LaravelLegends\PtBrValidator\Rules\Telefone::class
\LaravelLegends\PtBrValidator\Rules\TelefoneComDdd::class
\LaravelLegends\PtBrValidator\Rules\TelefoneComCodigo::class
\LaravelLegends\PtBrValidator\Rules\FormatoCep::class
\LaravelLegends\PtBrValidator\Rules\FormatoPlacaDeVeiculo::class
\LaravelLegends\PtBrValidator\Rules\FormatoPis::class
\LaravelLegends\PtBrValidator\Rules\Pis::class
\LaravelLegends\PtBrValidator\Rules\CpfOuCnpj::class
\LaravelLegends\PtBrValidator\Rules\FormatoCpfOuCnpj::class
\LaravelLegends\PtBrValidator\Rules\Uf::class

例如,如果您想验证一个CPF字段的格式,您可以使用以下方式使用LaravelLegends\PtBrValidator\Rules\FormatoCpf

use Illuminate\Http\Request;
use LaravelLegends\PtBrValidator\Rules\FormatoCpf;

// testando?cpf=valor_invalido

Route::get('testando', function (Request $request) {

    try{

        $dados = $request->validate([
            'cpf'  => ['required', new FormatoCpf]
            // outras validações aqui
        ]);

    } catch (\Illuminate\Validation\ValidationException $e) {
        dd($e->errors());
    }

});

变更日志

  • 9.1.0 - 添加了cns(国家健康卡)验证。
  • 8.0.3 - 添加了uf验证。
  • 8.0.2 - 添加了cpf_ou_cnpj验证。
  • 5.2.1 - 添加了cpf_ou_cnpj验证。

建议

Eloquent Filter:这个库是为了轻松创建REST API的搜索过滤器而开发的。使用这个库,您将节省大量代码行,同时保持Laravel应用中搜索过滤器的全局标准。

捐赠

Paypal