PHP code example of werx / validation

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

    

werx / validation example snippets

 php
e werx\Validation\Validator;

$input = 'foo';
$valid = Validator::minlength($input, 4);
var_dump($valid);

/*
bool(true)
*/
 php
use werx\Validation\Engine as ValidationEngine;

$validator = new ValidationEngine;
 php
$validator->addRule('firstname', 'First Name', '
 php
$valid = $validator->validate($_POST);
 php
$validator->addRule('volunteer.name', 'Name', 'ess', '
 php
$closure = function ($data, $id, $label) {
	$message = null;
	$success = $data[$id] == 'Foo';
	
	if (!$success) {
		$message = sprintf('%s must equal "Foo"', $label);
	}
	
	return [$success, $message];
};

$validator->addRule('firstname', 'First Name', $closure);

$valid = $validator->validate($_POST);
 php
namespace your\namespace\Rulesets;

use werx\Validation\Ruleset;

class Contact extends Ruleset
{
	public function __construct()
	{
		$this->addRule('firstname', 'First Name', '
}
 php
$contact_rules = new your\namespace\Rulesets\Contact;
$validator->addRuleset($contact_rules);
$valid = $validator->validate();
 php
if (!$valid) {
	$error_fields = $validator->getErrorFields();
}

/*
Array
(
	[0] => firstname
	[1] => lastname
)
*/
 php

$validator->addRule('firstname', 'First Name', 'ed');
$validator->addRule('age', 'Age', '	[2] => age
)
*/