PHP code example of iqomp / validator

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

    

iqomp / validator example snippets


use Iqomp\Validator\Validator;

$rules = [
    'name' => [
        'rules' => [
            'object)['name' => 'User'];
list($result, $errors) = Validator::validate($rules, $object);

    // ...
    'rules' => [
        'array' => true // 'assoc', 'indexed'
    ]
    // ...

    // ...
    'rules' => [
        'boolean' => true
    ]
    // ....

    // ...
    'rules' => [
        'callback' => 'Namespace\\Class::method'
    ]
    // ...

    // ...
    'rules' => [
        'date' => [
            'format' => 'Y-m-d',
            'min' => 'now',
            // 'min-field' => 'date-start',
            'max' => '+12 days',
            // 'max-field' => 'date-end'
        ]
    ]
    // ...

    // ...
    'rules' => [
        'email' => true
    ]
    // ...

    // ...
    'rules' => [
        'empty' => false
    ]
    // ...

    // ...
    'rules' => [
        'equals_to' => 'new-password'
    ]
    // ...

    // ...
    'rules' => [
        'file' => true
    ]
    // ...

    // ...
    'rules' => [
        'in' => ['one','two','three']
    ]
    // ...

    // ...
    'rules' => [
        'ip' => true // '4' | '6'
    ]
    // ...

    // ...
    'rules' => [
        'json' => true
    ]
    // ...

    // ...
    'rules' => [
        'length' => [
            'min' => 1,
            'max' => 12
        ]
    ]
    // ...

    // ...
    'rules' => [
        'notin' => ['one','two','three']
    ]
    // ...

    // ...
    'rules' => [
        // 'numeric' => true,
        'numeric' => [
            'min' => 1,
            'max' => 12
        ]
    ]
    // ...

    // ...
    'rules' => [
        'object' => true
    ]
    // ...

    // ...
    'rules' => [
        'regex' => '![0-9]+!'
    ]
    // ...

    // ...
    'rules' => [
        '

    // ...
    'rules' => [
        'req_on' => [
            'other_field' => [
                'operator' => '=',
                'expected' => 12
            ],
            'more_field' => [
                'operator' => 'in',
                'expected' => ['one','two','three']
            ]
        ]
    ]
    // ...

    // ...
    'rules' => [
        'text' => true,
        // 'text' => 'slug',
        // 'text' => '!^TR-[0-9]+$!'
    ]
    // ...

    // ...
    'rules' => [
        'url' => true,
        'url' => [
            'path' => true,
            'query' => true,
            'query' => ['page','rpp','q']
        ]
    ]
    // ...

    // ...
    'filters' => [
        'array' => true
    ]
    // ...

    // ...
    'filters' => [
        'boolean' => true
    ]
    // ...

    // ...
    'filters' => [
        'float' => true
    ]
    // ...

    // ...
    'filters' => [
        'integer' => true
    ]
    // ...

    // ...
    'filters' => [
        'json_encode' => true
    ]
    // ...

    // ...
    'filters' => [
        'lowercase' => true
    ]
    // ...

    // ...
    'filters' => [
        'object' => true
    ]
    // ...

    // ...
    'filters' => [
        // 'round' => true,
        'round' => 2
    ]
    // ...

    // ...
    'filters' => [
        'string' => true
    ]
    // ...

    // ...
    'filters' => [
        'ucwords' => true
    ]
    // ...

    // ...
    'filters' => [
        'uppercase' => true
    ]
    // ...



namespace MyModule\Module;

class Validator
{
    public static function custom(
        mixed  $value,
        mixed  $options,
        object $object,
        string $fname,
        array  $rules
    ) {
        // most of the time, the null value is for `a'=>'b']];
        // return ['100.0', ['a'=>'b'], 'Error message'];
    }
}


    // ...
    'rules' => [
        'custom' => 'awesome'
    ]
    // ...



// ...
    return [
        'validator' => [
            // error translation key
            'errors' => [
                '/code/' => '/translation key',
                '100.0' => 'the value is not accepted'
            ],
            'validators' => [
                'custom' => 'MyModule\\Module\\Validator::custom'
            ]
        ]
    ];
// ...



return [
    'the value is not accepted' => 'The value is not accepted.'
];



namespace MyModule\Module;

class Filter
{
    public static function custom(
        mixed  $value,
        mixed  $options,
        object $object,
        string $fname,
        array  $rules
    ) {
        // most of the time, it's not processed if it's null.
        if (is_null($value)) {
            return null;
        }

        // modify the $value;

        return $value;
    }
}



// ...
    return [
        'validator' => [
            'filters' => [
                'custom' => 'MyModule\\Module\\Filter::custom'
            ]
        ]
    ];
// ...



namespace MyModule\Formatter;

use Iqomp\Validator\ErrorFormatterInterface;

class MyErrorFormatter implements ErrorFormatterInterface
{
    public static function format(object $err, string $fld, array $errs, object $obj)
    {
        return [$err->text];
    }
}



return [
    'formatter' => 'MyModule\\Formatter\\MyErrorFormatter'
];



use MyModule\Formatter\MyErrorFormatter;
use Iqomp\Validator\Validator;

Validator::setErrorFormatter(MyErrorFormatter::class);

use Iqomp\Validator\Form;

$f_params = [
    'param1' => 'id',
    'param2' => 1
];

$form   = new Form('form-name', $f_params);
$result = $form->validate();

if (!$result) {
    $errors = $form->getErrors();
}



return [
    'forms' => [
        '/form-name/' => [
            '/field/' => [
                'rules' => [
                    // list of rules
                    'some-rule' => [
                        '$.param1' => '$.param2'
                    ]
                ],
                'filters' => [
                    // list of filters
                ]
            ]
        ]
    ]
];

bash
php bin/hyperf.php vendor:publish iqomp/validator