PHP code example of khalyomede / validator

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

    

khalyomede / validator example snippets


$validator = new Validator([
  'name' => [' ['string']
]);

$validator->validate([
  'name' => 'John', 
  'hobbies' => ['programming', 'comics', 'workout']
]);

var_dump($validator->failed()); // false



use Khalyomede\Validator;
use Khalyomede\Exception\RuleNotFoundException;

$validator = new Validator([
  'name' => ['string']
]);

try {
  $validator->validate(['name' => 'John']);

  var_dump($validator->failed()); // false
}
catch( RuleNotFoundException $exception ) {
  echo "rule {$exception->getRule()} does not exists";

  exit(1);
}



use Khalyomede\Validator;
use Khalyomede\Exception\RuleNotFoundException;

$validator = new Validator(['name' => ['Exception $exception ) {
  echo "rule {$exception->getRule()} does not exist";

  exit(1);
}

equire __DIR__ . '/../vendor/autoload.php';

use Khalyomede\Validator;
use Khalyomede\Exception\RuleAlreadyExistException;
use Khalyomede\Exception\RuleNotFoundException;

try {
  Validator::extends('longitude', function($value, $key, $items) {
    return is_float($value) && ($value >= -180) && ($value <= 180);
  });
}
catch( RuleAlreadyExistException $exception ) {
  echo "rule {$exception->getRule()} already exist";

  exit(1);
}



use Khalyomede\Validator;

if (Validator::has('ip') === false) {
  echo "rule ip does not exist yet";
}



use Khalyomede\Validator;

$validator = new Validator([
  'sith' => ['h Maul', 'Darth Vador', 'Darth Sidious']
]);

var_dump( $validator->failed() ) // "false", hm... should have been true after all these guys did but anyway

$validator = new Validator([
  'hobbies' => ['array']
]);

$validator = new Validator([
  'created_at' => ['date']
]);

$validator = new Validator([
  'updated_at' => ['datetime']
]);

$validator = new Validator([
  'contact' => ['email']
]);

$validator = new Validator([
  'name' => ['filled']
]);

$validator = new Validator([
  'age' => ['integer']
]);

$validator = new Validator([
  'street' => ['lower']
]);

$validator = new Validator([
  'lastname' => ['present']
]);


$validator = new Validator([
  'name' => ['

$validator = new Validator([
  'password' => ['string', 'same:confirmation'],
  'confirmation' => ['string']
]);

$validator = new Validator([
  'title' => ['slug']
]);

$validator = new Validator([
  'name' = ['string']
]);

$validator = new Validator([
  'duration' => ['time']
]);

$validator = new Validator([
  'name' => ['upper']
]);

Validator::extends('jedi', function($value, $key, $items) {
  return in_array($value, ['qui-gon jinn', 'obiwan', 'luke']);
});

Validator::has('sith'); // bool(false)