PHP code example of singra / laravel-br-validation

1. Go to this page and download the library: Download singra/laravel-br-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/ */

    

singra / laravel-br-validation example snippets


use Singra\LaravelBrValidation\Casts\AsCpf;
use Singra\LaravelBrValidation\Rules\CpfRule;

$request->validate([
    'document' => ['::class];
}

$holder->document;               // Singra\BrValidation\Documents\Cpf
$holder->document->formatted();  // '111.444.777-35'
$holder->document->redacted();   // '***.444.777-**'

'document' => ['

'document' => 'nullable|cpf',   // null passes
'document' => 'cpf',            // null fails

'document' => 'cpf',   // 12345678909 as a JSON number → fails

$request->validate(
    ['document' => 'válido.'],
);

use Singra\LaravelBrValidation\Casts\AsCep;
use Singra\LaravelBrValidation\Casts\AsCnpj;
use Singra\LaravelBrValidation\Casts\AsCpf;

protected function casts(): array
{
    return [
        'document' => AsCpf::class,
        'company'  => AsCnpj::class,
        'postcode' => AsCep::class,
    ];
}

$holder->document = '111.444.777-35';        // stored as '11144477735'
$holder->document = Cpf::from('11144477735') // likewise
$holder->document = null;                    // empty column
$holder->document = '11144477730';           // throws InvalidDocument

Cpf::isValid('11144477700');   // false
Cpf::mask('11144477700');      // '111.444.777-00'
Cpf::redact('11144477700');    // '***.444.777-**'

fake()->cpf();     // '11144477735'
fake()->cnpj();    // '11222333000181'
fake()->cep();     // '01310100'

fake()->cpf(formatted: true);          // '111.444.777-35'
fake()->cpf(region: FiscalRegion::Eighth);
fake()->cnpj(alphanumeric: true);      // 'K9VWWFYR000134'
fake()->cep(uf: Uf::PE);
bash
php artisan vendor:publish --tag=br-validation-lang