fonseca/cpf_cnpj_validation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fonseca/cpf_cnpj_validation

Composer 安装命令:

composer require fonseca/cpf_cnpj_validation

包简介

CPF and CNPJ validator for laravel applications.

README 文档

README

Latest Version on Packagist Software License Total Downloads Build Status

Introduction

This package provides a simple way to validate CPF and CNPJ for Laravel applications.

Requirements

  • PHP >= 8.0.2
  • Laravel >= 9.*

Installation

You can install the library via Composer:

composer require fonseca/cpf_cnpj_validation

Usage Guide

Add this code to your App\Providers\AppServiceProvider::class.

<?php

namespace App\Providers;

use Fnsc\RegistrationNumber\Validator as RegistrationNumber;
use Fnsc\CPF\Validator as CPF;
use Fnsc\CNPJ\Validator as CNPJ;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    private array $rules = [
        CPF::class,
        CNPJ::class,
        RegistrationNumber::class,
    ];

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerRules();
    }

    private function registerRules(): void
    {
        foreach ($this->rules as $rule) {
            $alias = (new $rule)->getAlias();
            Validator::extend($alias, $rule . '@passes');
        }
    }
}

Add these code to resources/lang/en/validation.php file.

return [
    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */
    ...
    'cpf' => 'The :attribute is invalid.',
    'cnpj' => 'The :attribute is invalid.',
    'registration_number' => 'The :attribute is invalid.',
    ...
];

And, finally, on your FooRequest.php file. Here you can choose which rule will be used.
The Fnsc\RegistrationNumber\Validator as RegistrationNumber; class made both validations, depending on the size of the string received.
The Fnsc\CPF\Validator as CPF; made only cpf validations, and Fnsc\CNPJ\Validator as CNPJ; made only cnpj validations.

public function rules()
{
    return [
        'registration_number' => 'required|registration_number',
        'cpf' => 'required|cpf',
        'cnpj' => 'required|cnpj',
    ];
}

License

This package is free software distributed under the terms of the MIT license

统计信息

  • 总下载量: 188
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固