Wallogit.com
2017 © Pedro Peláez
Validation and filtration for values such as form input.
A validation component, optimized multi-byte (i.e. Japanese language) support., (*1)
Features includes, such as:, (*2)
MIT License, (*3)
Use composer. only dev-master is available..., (*4)
"require": {
"wscore/validation": "dev-master"
}
This package almost works like this., (*5)
Use ValidationFactory to start validating an array.
For instance,, (*6)
use \WScore\Validation\ValidationFactory;
$factory = new ValidationFactory(); // use English rules and messages.
$factory = new ValidationFactory('ja); // use Japanese rules and messages.
to validate an array,, (*7)
$input = $factory->on($_POST);
$input->asText('name');
$input->asInteger('age');
$input->asDate('bate');
if($v->fails()) {
$messages = $input->messages();
}
$values = $input->get();
The as{Type}($name) method sets default rules for the input $name. Use method chaing to add more rules., (*8)
$input = $factory->on( $_POST ); // get validator.
$input->asText('name')->required() );
$input->asMail('mail')->required()->sameWith('mail2'));
$found = $input->get(); // [ 'name' => some name... ]
if( $input->fails() ) {
$onlyGoodData = $input->getSafe();
$containsBadData = $input->get();
} else {
$onlyGoodData = $input->get();
}
Check if the validation failes or not using fails()
method (or passess() method)., (*9)
To retrieve the validated (or unvalidated) values
by get() method, which returns all the values
(validated and invalidated values)., (*10)
To retrieve only the validated values,
use getSafe() method., (*11)
Use verify method to validate a single value., (*12)
$name = $input->verify( 'WScore', Rules::text()->string('lower') ); // returns 'wscore'
if( false === $input->verify( 'Bad', Rules::int() ) { // returns false
echo $input->result()->message(); // echo 'not an integer';
}
Validating an array of data is easy. When the validation fails, it returns the error message as array., (*13)
$input->source( array( 'list' => [ '1', '2', 'bad', '4' ] ) );
$input->asInteger('list');
if( !$input->passes() ) {
$values = $validation->get('list');
$goods = $validation->getSafe();
$errors = $validation->message();
}
/*
* $values = [ '1', '2', 'bad', '4' ];
* $goods = array( 'list' => [ '1', '2', '4' ] );
* $errors = array( 'list' => [ 2 => 'not an integer' ] );
*/
to treat separate input fields as one input, such as date., (*14)
$input->source( [ 'bd_y' => '2001', 'bd_m' => '09', 'bd_d' => '25' ] ); echo $validation->is( 'bd', Rules::date() ); // 2001-09-25
use multiple rules to construct own multiple inputs as,, (*15)
$input->asText('ranges')->multiple( [
'suffix' => 'y1,m1,y2,m2',
'format' => '%04d/%02d - %04d/%02d'
] );
where suffix lists the postfix for the inputs,
and format is the format string using sprintf., (*16)
for password or email validation with two input fields to compare each other., (*17)
$input->source([ 'text1' => '123ABC', 'text2' => '123abc' ] );
echo $validation->asText('text1')
->string('lower')
->sameWith('text2') ); // 123abc
Please note that the actual input strings are different. They become identical after lowering the strings., (*18)
some filter must be applied in certain order..., (*19)
echo $validate->verify( 'ABC', Rules::text()->pattern('[a-c]*')->string('lower'); // 'abc'
## should lower the string first, then check for pattern...
Use a closure as custom validation filter., (*20)
/**
* @param ValueTO $v
*/
$filter = function( $v ) {
$val = $v->getValue();
$val .= ':customized!';
$v->setValue( $val );
$v->setError(__METHOD__);
$v->setMessage('Closure with Error');
};
$input->asText('test')->addCustom( 'myFilter', $filter );
You cannot pass parameter (the closure is the parameter). argument is the ValueTO object which can be used to handle error and messages., (*21)
setting error with, well, actually, any string,
but __METHOD__ maybe helpful. this will break the
filter loop, i.e. no filter will be evaluated., (*22)
Error message is determined as follows:, (*23)
for tailored message, use message method to set its messag.e, (*24)
$validate->verify( '', $rule('text')->required()->message('Oops!') );
echo $validate->result()->message(); // 'Oops!'
filter, matches has its message based on the parameter., (*25)
$validate->verify( '', Rules::text()->required()->matches('code') );
echo $validate->result()->message(); // 'only alpha-numeric characters'
filters such as required and sameWith has message.
And lastly, there is a generic message for general errors., (*26)
$validate->verify( '', $rule('text')->required() );
echo $validate->result()->message(); // 'required input'
$validate->verify( '', Rules::date()->required() ); echo $validate->result()->message(); // 'invalid date'
uses generic message, if all of the above rules fails., (*27)
$validate->verify( '123', Rules::text()->pattern('[abc]') );
echo $validate->result()->message(); // 'invalid input'
todo: to-be-written, (*28)
todo: to-be-write, (*29)