PHP code example of igniphp / validation
1. Go to this page and download the library: Download igniphp/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/ */
igniphp / validation example snippets
use Igni\Validation\Assertion;
$numberValidator = Assertion::number($min = 0);
$numberValidator->validate(1);// true
$numberValidator->validate(-1);// false
$numberValidator->validate(1.0);// true
$numberValidator->validate('a'); // false
use Igni\Validation\Assertion;
use Igni\Validation\Error;
$userValidator = Assertion::group([
'name' => Assertion::alnum(),
'age' => Assertion::number(1, 200),
'email' => Assertion::email(),
'address' => Assertion::text(),
]);
$userValidator->validate([
'name' => 'John',
'age' => 233,
'email' => 'johnmail',
]);// false
$errors = $userValidator->getErrors();
$errors[0] instanceof Error\OutOfRangeError;// true
$errors[0]->getContext()->getName();//age
$errors[1] instanceof Error\EmptyValueError;// true
$errors[1]->getContext()->getName();//address
// Exception can also be factored out of failure instance
throw $errors[0]->toException();
use Igni\Validation\Assertion;
$validator = Assertion::alnum($minLength = 2);
var_dump($validator->validate('a1')); // true
use Igni\Validation\Assertion;
$validator = Assertion::alpha($minLength = 2);
var_dump($validator->validate('aaa')); // true
use Igni\Validation\Assertion;
$validator = Assertion::boolean();
var_dump($validator->validate(false)); // true
use Igni\Validation\Assertion;
$validator = Assertion::chain(Assertion::text(), Assertion::date());
var_dump($validator->validate('2018-09-10')); // true
use Igni\Validation\Assertion;
$validator = Assertion::contains('example');
var_dump($validator->validate('Test example')); // true
use Igni\Validation\Assertion;
$validator = Assertion::date('Y-m-d');
var_dump($validator->validate('2018-09-10')); // true
use Igni\Validation\Assertion;
$validator = Assertion::email();
var_dump($validator->validate('[email protected] ')); // true
use Igni\Validation\Assertion;
$validator = Assertion::each(Assertion::date('Y-m-d'));
var_dump($validator->validate(['2019-01-01', '2018-10-11'])); // true
use Igni\Validation\Assertion;
$validator = Assertion::falsy();
var_dump($validator->validate('no')); // true
use Igni\Validation\Assertion;
$validator = Assertion::group([
'email' => Assertion::email(),
'password' => Assertion::text(),
'date_of_birth' => Assertion::date('Y-m-d'),
]);
var_dump($validator->validate([
'email' => '[email protected] ',
'password' => 'secret',
'date_of_birth' => '2019-01-01',
])); // true
use Igni\Validation\Assertion;
$validator = Assertion::regex('^-[a-z]+$');
var_dump($validator->validate('-aa')); // true
use Igni\Validation\Assertion;
$validator = Assertion::truthy();
var_dump($validator->validate('yes')); // true
use Igni\Validation\Assertion;
$validator = Assertion::text($minLength = 2);
var_dump($validator->validate('aaa')); // true
use Igni\Validation\Assertion;
$validator = Assertion::in('no', 'yes', 'test');
var_dump($validator->validate('no')); // true
use Igni\Validation\Assertion;
$validator = Assertion::integer(10, 100);
var_dump($validator->validate(11)); // true
use Igni\Validation\Assertion;
$validator = Assertion::ip();
var_dump($validator->validate('123.123.123.123')); // true
use Igni\Validation\Assertion;
$validator = Assertion::ipv4();
var_dump($validator->validate('123.123.123.123')); // true
use Igni\Validation\Assertion;
$validator = Assertion::ipv6();
var_dump($validator->validate('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true
use Igni\Validation\Assertion;
$validator = Assertion::number(10, 100);
var_dump($validator->validate('11.2')); // true
use Igni\Validation\Assertion;
$validator = Assertion::uuid();
var_dump($validator->validate('1ff60619-81cc-4d8e-88ac-a3ae36a97dce')); // true
use Igni\Validation\Assertion;
$validator = Assertion::uri();
var_dump($validator->validate('/some/uri')); // true
use Igni\Validation\Assertion;
$validator = Assertion::uri();
var_dump($validator->validate('http://domain.com/some/uri')); // true
use Igni\Validation\Assertion;
$validator = Assertion::group([
'name' => Assertion::text(),
'age' => Assertion::integer(1, 200),
'email' => Assertion::email(),
]);
var_dump($validator->validate(['name' => 'John Doe', 'age' => 29, 'email' => '[email protected] '])); // true
declare(strict_types=1);
use Igni\Validation\Assertion;
class ValidateIn extends Assertion
{
public function __construct(...$values)
{
$this->attributes['valid_values'] = $values;
}
protected function assert($input): bool
{
return in_array($input, $this->attributes['valid_values'], $strict = true);
}
}