PHP code example of fuelphp / validation

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

    

fuelphp / validation example snippets




use Fuel\Validation\Validator;

// Create a new validator instance to play with
$v = new Validator;

// Set up our   ->addField('age', 'Current Age')
    ->number();

// Create some dummy data to validate
$data = array(
    'name' => 'John',
    'email' => '[email protected]',
    'age' => 32,
);

// Perform the validation
$result = $v->run($data);

var_dump($result->isValid()); // true
var_dump($result->getValidated()); // List of all the fields that passed validation




use Fuel\Validation\Validator;

// Create a new validator instance to play with
$v = new Validator;

// Set up our   ->addField('age', 'Current Age')
    ->number();

// Create some dummy data to validate
$data = array(
    'email' => 'john',
    'age' => 'not a number',
);

// Perform the validation
$result = $v->run($data);

var_dump($result->isValid()); // false
var_dump($result->getValidated()); // array()

var_dump($result->getErrors()); // returns an array of all the error messages encountered
var_dump($result->getError('name')); // Returns the error message for the 'name' field


use Fuel\Validation\Validator;

$v = new Validator;

$v->addField('name', 'User Name')
    ->

use Fuel\Validation\Validator;

// Create a new validator instance to play with
$v = new Validator;

$v->setGlobalMessage('



use Fuel\Validation\Validator;

// Create a new validator instance to play with
$v = new Validator;

$v->addCustomRule('myCustomRule', 'My\App\Rules\CustomRule');

// Example of adding the new rule via magic method syntax
$v->addField('foobar')
    ->myCustomRule();

$instance = $v->getRuleInstance('myCustomRule');
var_dump($instance); // instance of My\App\Rules\CustomRule



use Fuel\Validation\Validator;

// Create a new validator instance to play with
$v = new Validator;

$v->addCustomRule('stance('



use Fuel\Validation\Validator;
use Fuel\Validation\RuleProvider\FromArray;

// The key is the name of the field that has a value of an array containing the rules
$config = array(
    'name' => array(
        'reater
    ),

    // The exact parameters for each rule are documented with the rule itself and can differ between rules.
);

$v = new Validator;

$generator = new FromArray;
$generator->setData($config)->populateValidator($v);

// $v is now populated with the fields and rules specified in the config array.



use Fuel\Validation\Validator;
use Fuel\Validation\RuleProvider\FromArray;

$config = array(
    'name' => array(
        'myCustomRule',
    ),
);

$v = new Validator;

$v->addRule('myCustomRule', 'My\App\Rules\CustomRule');

$generator = new FromArray;
$generator->setData($config)->populateValidator($v);




use Fuel\Validation\Validator;
use Fuel\Validation\RuleProvider\FromArray;

// The key is the name of the field that has a value of an array containing the rules
$config = array(
    'name' => array(
        'label' => 'Name'
        'validation' => array(
            '     'numericMin' => 18,
        ),
    ),
);

$v = new Validator;

// First parameter: label key, default: disabled
// Second parameter: rules key, default: rules
$generator = new FromArray(true, 'validation'); // same as new FromArray('label', 'validation');
$generator->setData($config)->populateValidator($v);

// $v is now populated with the fields and rules specified in the config array.



$childValidator = new Validator();
$childValidator
    ->addField('test')
    ->maxLength(5);

$parentValidator = new Validator();
$parentValidator
    ->addField('child')
    ->validator($childValidator);

$parentValidator
    ->addField('foobar')
    ->