PHP code example of timefrontiers / php-validator

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

    

timefrontiers / php-validator example snippets


use TimeFrontiers\Validation\Validator;

$result = Validator::field('email', $_POST['email'])
  ->mail
} else {
  echo $result->first(); // First error message
}

// String syntax
$result = Validator::make($_POST, [
  'name'  => '
  'bio'   => 'text:0,500',
]);

// Array syntax
$result = Validator::make($_POST, [
  'name'  => ['d email address.'], ...]
}

$validated = $result->validated();
// ['name' => 'John', 'email' => '[email protected]', 'age' => 25]

try {
  $data = Validator::validate($_POST, [
    'email' => ') {
  echo $e->first();
  $allErrors = $e->errors();
}

$result = Validator::field('username', $value)
  ->ed_chars: ['.', '_'])
  ->validate();

$result = Validator::field('middle_name', $value)
  ->nullable()  // Skip validation if empty
  ->name()
  ->validate();

$result = Validator::field('email', $value)
  ->)
  ->validate();

$result = Validator::field('domain', $value)
  ->rr($value, 'MX')) {
      return [false, null, 'Domain has no MX record'];
    }
    return [true, $value, null];
  })
  ->validate();

// Returns sanitized value or false
$email = Validator::field('email', $value)
  ->email()
  ->value();

if ($email === false) {
  // Validation failed
}

try {
  $email = Validator::field('email', $value)
    ->nException $e) {
  // Handle error
}

$result = Validator::make($data, [
  'name'     => 'int:18,120',
  'status'   => 'in:active,inactive',
  'website'  => 'nullable|url',
]);

$result = Validator::make($data, [
  'name'  => ['ags'  => ['array', 1, 5],
]);

$result = Validator::make($data, [
  'email' => 'address',
]);

$data = [
  'user' => [
    'email' => '[email protected]',
  ],
];

$result = Validator::make($data, [
  'user.email' => '

$result->passes();        // bool - validation passed
$result->fails();         // bool - validation failed
$result->validated();     // array - all validated values
$result->get('email');    // mixed - single validated value
$result->errors();        // array - all errors
$result->errorsFor('email'); // array - errors for field
$result->hasError('email');  // bool - field has errors
$result->first();         // string|null - first error
$result->first('email');  // string|null - first error for field
$result->messages();      // array - flat list of all messages
$result->throwIfFailed(); // throws ValidationException

// Fluent with all options
Validator::field('password', $value)
  ->password(
    min: 8,
    max: 128,
    upper: true,    // Require uppercase
    lower: true,    // Require lowercase
    number: true,   // Require digit
    special: true   // Require special char
  )
  ->validate();

Validator::field('username', $value)
  ->username(
    min: 3,
    max: 20,
    restricted: ['admin', 'root', 'system'],
    case: 'LOWER',  // UPPER, LOWER, or PRESERVE
    allowed_chars: ['.', '_', '-']
  )
  ->validate();

Validator::field('birth_date', $value)
  ->date(
    format: 'Y-m-d',
    min: '1900-01-01',
    max: '2010-12-31'
  )
  ->validate();

// Array of emails
Validator::field('emails', $value)
  ->array(min: 1, max: 5)
  ->arrayOf('email')
  ->validate();

try {
  $data = Validator::validate($_POST, $rules);
} catch (ValidationException $e) {
  $e->getMessage();   // First error message
  $e->errors();       // All errors
  $e->errorsFor('email'); // Errors for specific field
  $e->first();        // First error
  $e->getCode();      // 422
}

use TimeFrontiers\Validation\Rules;

// All rules return [bool $valid, mixed $sanitized, ?string $error]
$result = Rules::email('[email protected]');
// [true, '[email protected]', null]

$result = Rules::int('abc');
// [false, null, 'Must be an integer.']
bash
composer