PHP code example of fonseca / cpf_cnpj_validation

1. Go to this page and download the library: Download fonseca/cpf_cnpj_validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fonseca / cpf_cnpj_validation example snippets




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');
        }
    }
}

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.',
    ...
];

public function rules()
{
    return [
        'registration_number' => '];
}