PHP code example of s-bhojani / validation
1. Go to this page and download the library: Download s-bhojani/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/ */
s-bhojani / validation example snippets
use Respect\Validation\Validator as v;
$number = 123;
v::numeric()->validate($number); //true
$usernameValidator = v::alnum()->noWhitespace()->length(1,15);
$usernameValidator->validate('alganet'); //true
$user = new stdClass;
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';
$userValidator = v::attribute('name', v::string()->length(1,32))
->attribute('birthdate', v::date()->minimumAge(18));
$userValidator->validate($user); //true
v::string()->notEmpty()->validate(''); //false input
v::not(v::int())->validate(10); //false, input must not be integer
$usernameValidator->validate('respect'); //true
$usernameValidator->validate('alexandre gaigalas'); //false
$usernameValidator->validate('#$%'); //false
try {
$usernameValidator->assert('really messed up screen#name');
} catch(\InvalidArgumentException $e) {
echo $e->getFullMessage();
}
try {
$usernameValidator->assert('really messed up screen#name');
} catch(\InvalidArgumentException $e) {
var_dump($e->findMessages(array('alnum', 'length', 'noWhitespace')));
}
$errors = $e->findMessages(array(
'alnum' => '{{name}} must contain only letters and digits',
'length' => '{{name}} must not have more than 15 chars',
'noWhitespace' => '{{name}} cannot contain spaces'
));
v::date('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
$hostnameValidator = v::zend('Hostname')->assert('google.com');
$timeValidator = v::sf('Time')->assert('22:00:01');
try {
$usernameValidator->check('really messed up screen#name');
} catch(\InvalidArgumentException $e) {
echo $e->getMainMessage();
}
v::allOf(
v::int(),
v::positive()
)->validate(15); //true
v::allOf(
v::int()->setName('Account Number'),
v::positive()->setName('Higher Than Zero')
)->setName('Positive integer')
->validate(15); //true
v::alnum()->validate('foo 123'); //true
v::alnum('-')->validate('foo - 123'); //true
v::alnum()->noWhitespace->validate('foo 123'); //false
v::alnum()->notEmpty()->validate(''); //false
v::alnum()->uppercase()->validate('aaa'); //false
v::arr()->validate(array()); //true
v::arr()->validate(new ArrayObject); //true
$obj = new stdClass;
$obj->foo = 'bar';
v::attribute('foo')->validate($obj); //true
v::attribute('foo', v::equals('bar'))->validate($obj); //true
v::attribute('lorem', v::string(), false)->validate($obj); // true
v::int()->between(10, 20)->validate(15); //true
v::string()->between('a', 'f')->validate('c'); //true
v::date()->between('2009-01-01', '2013-01-01')->validate('2010-01-01'); //true
v::date()->between('yesterday', 'tomorrow')->validate('now'); //true
v::date()->between(10, 20, true)->validate(20); //true
v::bool()->validate(true); //true
v::bool()->validate(false); //true
$url = 'http://www.google.com/search?q=respect.github.com'
$parts = parse_url($url);
v::arr()->key('scheme', v::startsWith('http'))
->key('host', v::domain())
->key('path', v::string())
->key('query', v::notEmpty());
v::call(
'parse_url',
v::arr()->key('scheme', v::startsWith('http'))
->key('host', v::domain())
->key('path', v::string())
->key('query', v::notEmpty())
)->validate($url);
v::call(array($myObj, 'methodName'), v::int())->validate($myInput);
v::call(function($input) {}, v::int())->validate($myInput);
v::callback('is_int')->validate(10); //true
v::charset('ASCII', 'açúcar'); //false
v::charset('ASCII', 'sugar'); //true
v::charset(array('ISO-8859-1', 'EUC-JP'), '日本国'); // true
v::consonant()->validate('xkcd'); //true
v::contains('ipsum')->validate('lorem ipsum'); //true
v::contains('ipsum')->validate(array('ipsum', 'lorem')); //true
v::cntrl()->validate("\n\r\t"); //true
v::countryCode('BR'); //true
v::cnh()->validate('02650306461');
v::cpf()->validate('44455566820');
v::cpf()->validate('444.555.668-20');
v::digit()->cpf()->validate('44455566820');
v::creditCard()->validate($myCredCardNumber);
v::digit()->creditCard()->validate($myCredCardNumber);
v::date()->validate('2009-01-01'); //true
v::date()->validate('now'); //true
v::date()->validate(new DateTime); //true
v::date('Y-m-d')->validate('01-01-2009'); //false
v::domain()->validate('google.com');
v::domain(false)->validate('dev.machine.local');
v::directory()->validate(__DIR__); //true
v::directory()->validate(__FILE__); //false
v::directory()->validate(new \SplFileInfo($directory));
$releaseDates = array(
'validation' => '2010-01-01',
'template' => '2011-01-01',
'relational' => '2011-02-05',
);
v::arr()->each(v::date())->validate($releaseDates); //true
v::arr()->each(v::date(), v::string()->lowercase())->validate($releaseDates); //true
v::email()->validate('[email protected] '); //true
v::exists()->validate(__FILE__); //true
v::exists()->validate(__DIR__); //true
v::exists()->validate(new \SplFileInfo($file));
v::endsWith('ipsum')->validate('lorem ipsum'); //true
v::endsWith('ipsum')->validate(array('lorem', 'ipsum')); //true
v::equals('alganet')->validate('alganet'); //true
v::equals(10)->validate('10'); //true
v::equals(10, true)->validate('10'); //false
v::int()->even()->validate(2); //true
v::file()->validate(__FILE__); //true
v::file()->validate(__DIR__); //false
v::file()->validate(new \SplFileInfo($file));
v::float()->validate(1.5); //true
v::float()->validate('1e5'); //true
v::graph()->validate('LKM@#$%4;'); //true
v::hexa()->validate('AF12'); //true
v::in('lorem ipsum')->validate('ipsum'); //true
v::in(array('lorem', 'ipsum'))->validate('lorem'); //true
v::instance('DateTime')->validate(new DateTime); //true
v::instance('Traversable')->validate(new ArrayObject); //true
v::int()->validate('10'); //true
v::int()->validate(10); //true
v::ip()->validate('192.168.0.1');
v::ip(FILTER_FLAG_NO_PRIV_RANGE)->validate('127.0.0.1'); //false
v::json->validate('{"foo":"bar"}'); //true
$dict = array(
'foo' => 'bar'
);
v::key('foo')->validate($dict); //true
v::key('foo', v::equals('bar'))->validate($dict); //true
v::key('lorem', v::string(), false)->validate($dict); // true
v::leapDate('Y-m-d')->validate('1988-02-29'); //true
v::leapYear()->validate('1988'); //true
v::string()->length(1, 5)->validate('abc'); //true
v::string()->length(5, null)->validate('abcdef'); // true
v::string()->length(null, 5)->validate('abc'); // true
v::arr()->length(1, 5)->validate(array('foo', 'bar')); //true
v::string()->length(1, 5, true)->validate('a'); //true
v::string()->lowercase()->validate('xkcd'); //true
v::macAddress()->validate('00:11:22:33:44:55'); //true
v::int()->max(15)->validate(20); //false
v::date()->max('2012-01-01')->validate('2010-01-01'); //true
v::int()->min(15)->validate(5); //false
v::date()->min('2012-01-01')->validate('2015-01-01'); //true
v::date()->minimumAge(18)->validate('1987-01-01'); //true
v::int()->multiple(3)->validate(9); //true
v::numeric()->negative()->validate(-15); //true
v::noWhitespace()->validate('foo bar'); //false
v::noWhitespace()->validate("foo\nbar"); //false
v::string()->noWhitespace()->validate(''); //true
v::string()->noWhitespace()->validate(' '); //false
v::noneOf(
v::int(),
v::float()
)->validate('foo'); //true
php
v::ip()->not()->validate('foo'); //true
php
v::int()->positive()->not()->validate(-1.5); //true
php
v::notEmpty()->validate(null); //false
php
v::int()->notEmpty()->validate(0); //false
php
v::arr()->notEmpty()->validate(array()); //false
php
v::nullValue()->validate(null); //true
php
v::numeric()->validate(-12); //true
v::numeric()->validate('135.0'); //true
php
v::int()->odd()->validate(3); //true
php
v::perfectSquare()->validate(25); //true (5*5)
v::perfectSquare()->validate(9); //true (3*3)
php
v::numeric()->positive()->validate(-15); //false
php
v::primeNumber()->validate(7); //true
php
v::prnt()->validate('LMKA0$% _123'); //true
php
v::punct()->validate('&,.;[]'); //true
php
v::readable()->validate('/path/of/a/readable/file'); //true
php
v::roman()->validate('IV'); //true
php
v::sf('Time')->validate('15:00:00');
php
v::space()->validate(' '); //true
php
v::startsWith('lorem')->validate('lorem ipsum'); //true
php
v::startsWith('lorem')->validate(array('lorem', 'ipsum')); //true
php
v::string()->validate('hi'); //true
php
v::uploaded()->validate('/path/of/an/uploaded/file'); //true
php
v::version()->validate('1.0.0');
php
v::vowel()->validate('aei'); //true
php
v::xdigit()->validate('abc123'); //true
php
v::xdigit()->validate('0x1f'); //false
php
v::writable()->validate('/path/of/a/writable/file'); //true
php
v::zend('Hostname')->validate('google.com');