$v = Validate::length(10, 20, true)->regex('/^[az]+$/i'); $v->validate('O'Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'value must have a length between 10 and 20', 'regex' => 'value contains invalid characters' ] */ $v->getFirstError(); // output: value must have a length between 10 and 20
$input = [ 'username' => 'O'Reilly', 'email' => 'o-reilly@site' ]; $attributes = [ 'username' => Validate::required() ->length(2, 20, true) ->regex('/^[az]+$/i'), 'email' => Validate::required()->email() ]; $v = Valiadte::attributes($attributes); $v->validate($input); // output: false $v->getErrors(); /* output: [ 'username' => [ 'regex' => 'value contains invalid characters', ], 'email' => [ 'email' => 'email must be valid', ], ] */
Validate::attributes(Validate::required()->string())->validate($input);
$v = Validate::notOf(Validate::required()); $v->validate(''); // output: true
$input = [ 'email' => 'tom@site', 'username' => '' ]; $attributes = Validate::attributes([ 'email' => Validate::email(), 'username' => Validate::required() ]); $v = Validate::notOf($attributes); $v->validate($input); // output: true
$input = 7; $v = Validate::oneOf(Validate::string()->email()); $v->validate($input); // output: false $v->getErrors(); /* output: [ 'string' => 'value must be string' ] */
$input = [ 'email' => 'tom@site', 'username' => '' ]; $attributes = Validate::attributes([ 'email' => Validate::email(), 'username' => Validate::required() ]); $v = Validate::oneOf($attributes); $v->validate($input); // output: false $v->getErrors(); /* output: [ 'email' => [ 'email' => 'email must be valid', ] ] */
v::when(v $if, v $then, v $else = null)
$v = Validate::when(Validate::equals('Tom'), Validate::numeric()); $v->validate('Tom'); // output false $v->getErrors(); /* output: [ 'numeric' => 'value must be numeric', ] */
$v = Validate::length(10, 20) ->regex('/^[az]+$/i') ->placeholders(['name' => 'username']); $v->validate('O'Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'username must have a length between 10 and 20', 'regex' => 'username contains invalid characters', ] */
$v = Validate::length(10, 20) ->regex('/^[az]+$/i') ->messages(['regex' => ', !']); $v->validate('O'Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'username must have a length between 10 and 20', 'regex' => ', !' ] */
$v = Validate::length(10, 20)->templates(['length' => Length::GREATER]); $v->validate('O'Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'value must have a length lower than 20', ] */
$v = Validate::locale('ru') ->length(10, 20) ->regex('/^[az]+$/i'); $v->validate('O'Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => ' 10 20', 'regex' => ' ', ] */
use rock\validate\rules\Rule class CSRF extends Rule { public function __construct($compareTo, $compareIdentical = false, $config = []) { $this->parentConstruct($config); $this->params['compareTo'] = $compareTo; $this->params['compareIdentical'] = $compareIdentical; } public function validate($input) { if ($this->params['compareIdentical']) { return $input === $this->params['compareTo']; } return $input == $this->params['compareTo']; } }
use rock\validate\locale\Locale; class CSRF extends Locale { const REQUIRED = 1; public function defaultTemplates() { return [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be valid', self::REQUIRED => '{{name}} must not be empty' ], self::MODE_NEGATIVE => [ self::STANDARD => '{{name}} must be invalid', self::REQUIRED => '{{name}} must be empty' ] ]; } public function defaultPlaceholders($compareTo) { if (empty($compareTo)) { $this->defaultTemplate = self::REQUIRED; } return [ 'name' => 'CSRF-token' ]; } }
$config = [ 'rules' => [ 'csrf' => [ 'class' => \namespace\to\CSRF::className(), 'locales' => [ 'en' => \namespace\to\en\CSRF::className(), ] ], ] ]; $sessionToken = 'foo'; $requestToken = 'bar'; $v = new Validate($config); $v->csrf($sessionToken)->validate($requestToken); // output: false $v->getErrors(); /* output: [ 'csrf' => 'CSRF-token must be valid', ] */
$v = Validate::email(); $v->validate(''); // output: true
$v->skipEmpty(false)->validate(''); // output: false
$config = [ 'rules' => [ 'custom' => [ 'class' => \namespace\to\CustomRule::className(), 'locales' => [ 'en' => \namespace\to\en\Custom::className(), ], 'isEmpty' => function($input){ return $input === ''; } ], ] ]; $v = new Validate($config);
composer require romeoz/rock-validate:*
docker run --name demo -d -p 8080:80 romeoz/docker-rock-validate
Source: https://habr.com/ru/post/253621/
All Articles