Skip to content

A simple package that validates EU VAT numbers against the central ec.europa.eu database

License

Notifications You must be signed in to change notification settings

CodeSkills/laravel-vat-eu-validator

 
 

Repository files navigation

Laravel VAT EU VALIDATOR

Latest Version on Packagist Build Status Total Downloads

laravel-vat-eu-validator is a package inspired from vat.php to validate a VAT number for businesses based in Europe.

Installation

You can install the package via composer:

composer require danielebarbaro/laravel-vat-eu-validator

The package will automatically register itself.

Lumen

As Lumen doesn't auto-discover service providers, You will need to register it in bootstrap/app.php:

$app->register(Danielebarbaro\LaravelVatEuValidator\VatValidatorServiceProvider::class);

Usage

use Danielebarbaro\LaravelVatEuValidator\Facades\VatValidatorFacade as VatValidator;

// Check VAT format and VIES existence
VatValidator::validate('IT12345');

// Check VAT format
VatValidator::validateFormat('IT12345678901'); 

// Check VAT existence
VatValidator::validateExistence('IT12345678901');

Validation

The package registers two new validation rules.

vat_number

The field under validation must be a valid and existing VAT number.

vat_number_exist

The field under validation check id is an existing VAT number.

vat_number_format

The field under validation must be a valid VAT number.

use Illuminate\Http\Request;

class Controller {

    public function foo(Request $request) 
    {
        $request->validate([
            'bar_field' => ['vat_number'],
        ]);
        
        $request->validate([
            'bar_field' => ['vat_number_exist'],
        ]);
        
        $request->validate([
            'bar_field' => ['vat_number_format'],
       ]);
    }
}

Alternatively, you can also use the Rule directly.

use Illuminate\Http\Request;
use Danielebarbaro\LaravelVatEuValidator\Rules;

class Controller {

    public function foo(Request $request) 
    {
        $request->validate([
            'bar_field' => [ new Rules\VatNumber() ],
            'bar_field' => [ new Rules\VatNumberExist() ],
            'bar_field' => [ new Rules\VatNumberFormat() ],
        ]);
    }
}

Translations

Just add and customize validation strings in resources/lang/en/validation.php

    ...
    'vat_number' => 'The :attribute must be a valid VAT number.',
    'vat_number_format' => 'The :attribute must be write in a valid number format {country_name}{vat_number}.',
    'vat_number_exist' => 'VAT number :attribute  not exist.',
    ...

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Contributors

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

About

A simple package that validates EU VAT numbers against the central ec.europa.eu database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%