2017 © Pedro Peláez
 

library validation

Validation and filtration for values such as form input.

image

wscore/validation

Validation and filtration for values such as form input.

  • Sunday, November 26, 2017
  • by asaokamei
  • Repository
  • 1 Watchers
  • 0 Stars
  • 65 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

Validation

A validation component, optimized multi-byte (i.e. Japanese language) support., (*1)

Features includes, such as:, (*2)

  • works well with code completion.
  • multiple values combined to a single value (ex: bd_y, bd_m, bd_d to bd).
  • preset order of rules to apply. essential to handle Japanese characters.
  • easy to code logic.

License

MIT License, (*3)

Installation

Use composer. only dev-master is available..., (*4)

"require": {
    "wscore/validation": "dev-master"
}

Simple Usage

This package almost works like this., (*5)

factory object

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();

validation rule

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)

validating a single value

rewrite-this-section.

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';
}

Advanced Features

validating array as input

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' ] );
 */

multiple inputs

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)

sameWith to compare values

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)

order of filter

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...

custom validation

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)

Predefined Messages

Error message is determined as follows:, (*23)

  1. message to specify by message rule,
  2. method and parameter specific message,
  3. method specific message,
  4. type specific message, then,
  5. general message

example 1) message to specify by message rule

for tailored message, use message method to set its messag.e, (*24)

$validate->verify( '', $rule('text')->required()->message('Oops!') );
echo $validate->result()->message(); // 'Oops!'

example 2) method and parameter specific message

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'

example 3 ) method specific message

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'

example 4) type specific message

$validate->verify( '', Rules::date()->required() );
echo $validate->result()->message(); // 'invalid date'

example 5) general message

uses generic message, if all of the above rules fails., (*27)

$validate->verify( '123', Rules::text()->pattern('[abc]') );
echo $validate->result()->message(); // 'invalid input'

Predefined Types

todo: to-be-written, (*28)

  • text
  • mail
  • number
  • integer
  • float
  • date
  • dateYM
  • datetime
  • time
  • timeHi
  • tel

Predefined Filters

todo: to-be-write, (*29)

  • message
  • multiple
  • noNull
  • encoding
  • mbConvert (Ja only)
  • trim
  • sanitize
  • string
  • default
  • required
  • loopBreak
  • code
  • maxlength
  • pattern
  • matches
  • kanaType (ja only)
  • etc.

The Versions

26/11 2017

2.x-dev

2.9999999.9999999.9999999-dev

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

25/11 2017

2.0.2

2.0.2.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

23/11 2017

2.0.1

2.0.1.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

12/11 2017

2.0.0

2.0.0.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

03/09 2016

2.0.0-beta.1

2.0.0.0-beta1

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

03/09 2016

dev-master

9999999-dev

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

07/10 2015

1.0.1

1.0.1.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

05/10 2015

1.0.0

1.0.0.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

26/03 2015

0.2.2

0.2.2.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

08/03 2015

0.2.1.1

0.2.1.1

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

08/03 2015

0.2.1

0.2.1.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

08/03 2015

0.2.0

0.2.0.0

Validation and filtration for values such as form input.

  Sources   Download

MIT

by Asao Kamei

25/07 2013

0.1.0

0.1.0.0

Validation and filtration for values such as form input.

  Sources   Download

by Asao Kamei