PHP code example of iamfarhad / validation

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

    

iamfarhad / validation example snippets


// Old way (still works)
'mobile' => ['ir_mobile']

// New modern way  
'mobile' => [new Mobile()]

// Both approaches are supported!

use Iamfarhad\Validation\Rules\{NationalCode, Mobile, ShamsiDate};

// Validate Iranian user registration
$validator = Validator::make($request->all(), [
    'first_name' => ['equired', new ShamsiDate()],
    'company_id' => ['nullable', 'ir_company_id'],
]);

if ($validator->fails()) {
    return response()->json(['errors' => $validator->errors()], 422);
}

use Illuminate\Support\Facades\Validator;
use Iamfarhad\Validation\Rules\{NationalCode, Mobile, ShamsiDate};

$validator = Validator::make($request->all(), [
    'national_code' => ['

use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'national_code' => ['red', 'domain'],
    'website' => ['

use Iamfarhad\Validation\Rules\{Mobile, NationalCode, PersianAlpha, ShamsiDate};

public function rules(): array
{
    return [
        'first_name' => ['w Mobile()],
        'birth_date' => ['nullable', new ShamsiDate()],
    ];
}

public function rules(): array
{
    return [
        'first_name' => ['_code' => [' 'shamsi_date'],
        'company_id' => ['nullable', 'ir_company_id'],
        'website' => ['nullable', 'url_format'],
        'username' => ['

// Shamsi date with custom separator
'birth_date' => ['ed', 'shamsi_date_between:1380,1420,/,true'], // startYear: 1380, endYear: 1420, separator: /, convertPersianNumbers: true

use Iamfarhad\Validation\Rules\{PersianAlpha, PersianNumber, PersianAlphaNum, PersianAlphaEngNum};

// Persian alphabet only
Validator::make(['name' => 'فارسی'], [
    'name' => [new PersianAlpha()]
]);

// Persian numbers only
Validator::make(['number' => '۱۲۳۴۵'], [
    'number' => [new PersianNumber()]
]);

// Persian alphabet + Persian numbers
Validator::make(['text' => 'فارسی۱۲۳'], [
    'text' => [new PersianAlphaNum()]
]);

// Persian alphabet + English numbers
Validator::make(['text' => 'فارسی123'], [
    'text' => [new PersianAlphaEngNum()]
]);

// Persian alphabet only
Validator::make(['name' => 'فارسی'], [
    'name' => ['persian_alpha']
]);

// Persian numbers only
Validator::make(['number' => '۱۲۳۴۵'], [
    'number' => ['persian_number']
]);

// Persian alphabet + Persian numbers
Validator::make(['text' => 'فارسی۱۲۳'], [
    'text' => ['persian_alpha_num']
]);

// Persian alphabet + English numbers
Validator::make(['text' => 'فارسی123'], [
    'text' => ['persian_alpha_eng_num']
]);

use Iamfarhad\Validation\Rules\Mobile;

// Supports all Iranian operators (Irancell, Rightel, Hamrah-e Avval, etc.)
Validator::make(['mobile' => '09123456789'], [
    'mobile' => [new Mobile()]
]);

// Iranian mobile validation
Validator::make(['mobile' => '09123456789'], [
    'mobile' => ['ir_mobile']
]);

use Iamfarhad\Validation\Rules\NationalCode;

// Validates Iranian national identification codes
Validator::make(['national_code' => '0112169228'], [
    'national_code' => [new NationalCode()]
]);

// Iranian national code validation
Validator::make(['national_code' => '0112169228'], [
    'national_code' => ['ir_national_code']
]);

use Iamfarhad\Validation\Rules\Sheba;

// Iranian bank account numbers (IBAN format)
Validator::make(['account' => 'IR930150000001351800087201'], [
    'account' => [new Sheba()]
]);

use Iamfarhad\Validation\Rules\Phone;
use Iamfarhad\Validation\Rules\PhoneArea;

// Landline numbers
Validator::make(['phone' => '22345678'], [
    'phone' => [new Phone()]
]);

// With area code
Validator::make(['phone_area' => '02122345678'], [
    'phone_area' => [new PhoneArea()]
]);

use Iamfarhad\Validation\Rules\CardNumber;

// Validates using Luhn algorithm
Validator::make(['card' => '4532015112830366'], [
    'card' => [new CardNumber()]
]);

use Iamfarhad\Validation\Rules\PersianAlphaNum;
use Iamfarhad\Validation\Rules\PersianAlphaEngNum;

// Persian alphabet and Persian numbers
Validator::make(['text' => 'فارسی۱۲۳'], [
    'text' => [new PersianAlphaNum()]
]);

// Persian alphabet and English numbers
Validator::make(['text' => 'فارسی123'], [
    'text' => [new PersianAlphaEngNum()]
]);

use Iamfarhad\Validation\Rules\CompanyId;

// Iranian company identifier
Validator::make(['company_id' => '14007650912'], [
    'company_id' => [new CompanyId()]
]);

use Iamfarhad\Validation\Rules\Url;
use Iamfarhad\Validation\Rules\Domain;

// URL validation
Validator::make(['website' => 'https://example.com'], [
    'website' => [new Url()]
]);

// Domain validation
Validator::make(['domain' => 'example.com'], [
    'domain' => [new Domain()]
]);

use Iamfarhad\Validation\Rules\ShamsiDate;
use Iamfarhad\Validation\Rules\ShamsiDateBetween;

// Basic Shamsi date
Validator::make(['date' => '1400/01/01'], [
    'date' => [new ShamsiDate()]
]);

// Shamsi date with custom separator
Validator::make(['date' => '1400-01-01'], [
    'date' => [new ShamsiDate('-')]
]);

// Shamsi date with Persian numbers
Validator::make(['date' => '۱۴۰۰/۰۱/۰۱'], [
    'date' => [new ShamsiDate('/', true)]
]);

// Shamsi date within year range
Validator::make(['date' => '1400/01/01'], [
    'date' => [new ShamsiDateBetween(1380, 1420)]
]);

use Iamfarhad\Validation\Rules\{CompanyId, Url, Domain};

// Iranian company identifier
Validator::make(['company_id' => '14007650912'], [
    'company_id' => [new CompanyId()]
]);

// URL validation
Validator::make(['website' => 'https://example.com'], [
    'website' => [new Url()]
]);

// Domain validation
Validator::make(['domain' => 'example.com'], [
    'domain' => [new Domain()]
]);

// Company and web validation
Validator::make($data, [
    'company_id' => ['ir_company_id'],
    'website' => ['url_format'],
    'domain' => ['domain'],
]);

use Iamfarhad\Validation\Rules\{ShamsiDate, ShamsiDateBetween};

// Basic Shamsi date
Validator::make(['date' => '1400/01/01'], [
    'date' => [new ShamsiDate()]
]);

// Custom separator and Persian number support
Validator::make(['date' => '۱۴۰۰-۰۱-۰۱'], [
    'date' => [new ShamsiDate('-', true)] // separator: -, convertPersianNumbers: true
]);

// Date between years
Validator::make(['event_date' => '1400/06/15'], [
    'event_date' => [new ShamsiDateBetween(1380, 1420)]
]);

// Shamsi date validation
Validator::make($data, [
    'birth_date' => ['shamsi_date'], // Default: separator='/', convertPersianNumbers=false
    'start_date' => ['shamsi_date:-,true'], // Custom separator and Persian number conversion
    'event_date' => ['shamsi_date_between:1380,1420,/,false'], // Between years 1380-1420
]);

use Iamfarhad\Validation\Rules\{Mobile, NationalCode, PersianAlpha, ShamsiDate};

public function rules(): array
{
    return [
        // Using ValidationRule objects
        'first_name' => ['equired', new ShamsiDate()],
        
        // Using string rules
        'company_id' => ['nullable', 'ir_company_id'],
        'website' => ['nullable', 'url_format'],
        'username' => ['

use Iamfarhad\Validation\Rules\{Mobile, NationalCode, PersianAlpha, ShamsiDate, CompanyId};

$validator = Validator::make($request->all(), [
    // Personal Information
    'first_name' => [' ShamsiDate()],
    
    // Business Information (String rules)
    'company_id' => ['nullable', 'ir_company_id'],
    'company_website' => ['nullable', 'url_format'],
    'company_domain' => ['nullable', 'domain'],
    
    // Account Information
    'username' => ['
bash
php artisan vendor:publish --provider="Iamfarhad\Validation\ValidationServiceProvider"