PHP code example of polycademy / validation

1. Go to this page and download the library: Download polycademy/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/ */

    

polycademy / validation example snippets


use Polycademy\Validation\Validator;
use Polycademy\Validation\Rule;

$validator = new Validator();
$validator
	->set_label('name', 'first name')
	->set_label('email', 'email address')
	->set_label('password2', 'password confirmation')
	->add_filter('name', 'trim')
	->add_filter('email', 'trim')
	->add_filter('email', 'strtolower')
	->add_rule('name', new Rule\MinLength(5))
	->add_rule('name', new Rule\MaxLength(10))
	->add_rule('email', new Rule\MinLength(5))
	->add_rule('email', new Rule\Email())
	->add_rule('password', new Rule\Matches('password2'))
;

if($validator->is_valid($_POST)) {
	print_r($validator->get_data());
} else {
	print_r($validator->get_errors());
}

use Polycademy\Validation\Validator;
use Polycademy\Validation\Rule;

$validator = new Validator();

$validator->setup_rules(array(
	'name' => array(
		'set_label:Course Name',
		'NotEmpty',
		'AlphaNumericSpace',
		'MinLength:5',
		'MaxLength:50',
	),
	'starting_date' => array(
		'set_label:Starting Date',
		'Regex:/^(19|20)\d\d\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/',
	),
	'days_duration' => array(
		'set_label:Course Duration',
		'NotEmpty',
		'Number',
		'NumRange:1,200',
	),
	'times' => array(
		'set_label:Course Times',
		'NotEmpty',
		'MinLength:1',
		'MaxLength:100',
		'AlphaNumericSpace',
	),
	'number_of_applications' => array(
		'set_label:Number of Applicants',
		'Number',
		'NumRange:0,100',
	),
	'number_of_students' => array(
		'set_label:Number of Students',
		'Number',
		'NumRange:0,100',
	),
));

if(!$validator->is_valid($data)){

	//returns array of key for data and value
	$errors = $validator->get_errors();
	
}