PHP code example of unicframework / validator

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

    

unicframework / validator example snippets


use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name' => [
    ' true,
    'string' => true
  ],
  'email' => [
    '> [
    '

use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name,last_name' => 'd|not_null|minlength:6'
]);

use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name' => [
    'e,
    'string' => true
  ],
  'email' => [
    '> [
    'tring' => 'First name should be in string'
  ],
  'last_name' => [
    's'
  ],
  'gender' => [
    '

use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name,last_name' => 't_null|minlength:6'
],
[
  'first_name,last_name' => 'can not be null|in:Please select valid gender',
  'password' => '

use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'ired' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'ta like arrays, objects, and json etc.
$data = [
  'name' => 'abc xyz',
  'gender' => 'male',
  'contact' => [
    'email' => '[email protected]'
  ]
];

// Validate data
if($validator->validate($data)) {
  //Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors();
}

use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'ired' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'ta like arrays, objects, and json etc.
$data = [
  [
    'name' => 'abc xyz',
    'gender' => 'male',
    'contact' => [
      'email' => '[email protected]'
    ]
  ],
  [
    'name' => 'xyz abc',
    'gender' => 'male',
    'contact' => [
      'email' => '[email protected]'
    ]
  ]
];

// Validate multiple sets of data
if($validator->validate($data, true)) {
  // Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors());
}

// Get all errors
$errors = $validator->errors();

// Get all valid data
$errors = $validator->getValidData();

// Get all invalid data
$errors = $validator->getInvalidData();

// Set validation rules
$validator = Validator::make([
  'email' => [
    'les
    'blocked' => function($value) {
      if($value == '[email protected]') {
        // Email [email protected] is blocked
        return false;
      } else {
        return true;
      }
    },
    // Set your own custom rules
    'available' => is_available($value),
  ]
],
[
  // Set error messages for custom rules
  'email' => [
    'blocked' => 'this email address is blocked',
    'available' => 'this email address is already registered',
  ]
]);