jekk0 / laravel-iso3166-validation-rules by Jekk0

Laravel/Lumen validation rules for validate country codes (ISO3166-1)
47,864
1
0
Package Data
Maintainer Username: Jekk0
Package Create Date: 2019-05-25
Package Last Update: 2024-11-01
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-15 15:24:16
Package Statistics
Total Downloads: 47,864
Monthly Downloads: 1,803
Daily Downloads: 83
Total Stars: 1
Total Watchers: 0
Total Forks: 2
Total Open Issues: 0

Laravel/ Lumen validation rules for Country Codes - ISO 3166-1

Build Status Coverage Status Latest Stable Version Total Downloads

Requirements

  • PHP >= 7.2
  • Laravel/Lumen 5.8.x

Installation

Install the latest version with

 $ composer require jekk0/laravel-iso3166-validation-rules

Quick start.

Using in controller

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Jekk0\laravel\Iso3166\Validation\Rules\Iso3166Alpha2;

class Test extends Controller
{
    public function index(Request $request)
    {
        $rules = ['country' => ['required', new Iso3166Alpha2()]]; // ISO3166-1 Alpha2 validation rule

        $this->validate($request, $rules);
        
        // etc ...
    }
}

Available rules

<?php
use Jekk0\laravel\Iso3166\Validation\Rules\Iso3166Alpha2;
use Jekk0\laravel\Iso3166\Validation\Rules\Iso3166Alpha3;
use Jekk0\laravel\Iso3166\Validation\Rules\Iso3166Numeric;

$rules = ['countryAlpha2' => ['required', new Iso3166Alpha2()]]; // ISO3166-1 Alpha2 validation rule
$rules = ['countryAlpha3' => ['required', new Iso3166Alpha3()]]; // ISO3166-1 Alpha3 validation rule
$rules = ['countryNumeric' => ['required', new Iso3166Numeric()]]; // ISO3166-1 Numeric validation rule

Customise error message

<?php
...
    public function index(Request $request)
    {
        $iso3166Alpha2Rule = (new Iso3166Alpha2())->setErrorMessage('New Custom Error Message :attribute = :input');
        $rules = ['country' => ['required', $iso3166Alpha2Rule]]; // ISO3166-1 Alpha2 validation rule

        $this->validate($request, $rules);

        // etc ...
    }
    
    // Output
    // New Custom Error Message country = ZZZ

Laravel/Lument automatically parse error message and replace:

  • :attribute -> form parameter name
  • :input -> form parameter value

Docs

  • Country Codes - ISO 3166: https://www.iso.org/iso-3166-country-codes.html
  • ISO 3166-1: https://en.wikipedia.org/wiki/ISO_3166-1