PHP code example of boomerang / validatr

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

    

boomerang / validatr example snippets


1

// Lets create our own 'equal' rule.

$data = array (
    'name' => 'ok'
);

$rules = array(
    'name' => 'custom_equal:OK:strict:upper'
);

$messages = array(
    'name' => array(
        'custom_equal' => 'The value must be equal "ok"'
    )
);

// The callback function may receives three arguments:
// 1st arg - field value - 'ok' (From $data)
// 2nd arg - rule value - 'OK' (From $rules 2nd param)
// 3rd arg - additional params - array (From $rules starting 3nd and more params)

// $params in this example is an array - array('strict', 'upper'); But not used
// It should return a boolean value indicating whether the value is valid.
$validator->addRule('custom_equal', function($dataValue, $ruleValue, $params){
    return ($dataValue == $ruleValue) ? true : false;
});

$result = $validator->validate($data, $rules, $messages);

$ php composer.phar