PHP code example of guyliangilsing / php-validation

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

    

guyliangilsing / php-validation example snippets


use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;

$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);

// Your configuration logic here...

$validator = $builder->build();

use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;

use function PHPValidation\Functions\ors([
    'field1' => [

// Passes a new validation handler/strategy to the actual validator
$newStrategy = // Your custom strategy here...
$builder->setStrategy($newStrategy);

// Registers a new validator class that will be returned when you build the validator
$builder->setValidatorClassName(Validator::class);

$validator = $builder->build();

namespace PHPValidation\Factories\ValidatorFactory;

$factory = new ValidatorFactory();
$validator = $factory->createDefaultValidator();

[
    'fieldName' => [FieldValidatorInterface, FieldValidatorInterface, FieldValidatorInterface],
    'nestedField' => [
        'fieldName' => [FieldValidatorInterface, FieldValidatorInterface, FieldValidatorInterface],
    ]
]

[
    'fieldName' => [
        'fieldValidatorKey' => "Your custom error message..."
    ],
    'nestedField' => [
        'fieldName' => [
            'fieldValidatorKey' => "Your custom error message..."
        ],
    ],
]

use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;

$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);

// Your configuration logic here...

$validator = $builder->build();

use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;

use function PHPValidation\Functions\ired()],
]);

$validator = $builder->build();

$isValid = $validator->isValid([]); // Will return false
$errorMessages = $validator->getErrorMessages(); // Will return error messages

declare(strict_types=1);

namespace PHPValidation\Fields;

final class RequiredField implements FieldValidatorInterface
{
    // Is used by the validation strategy and will skip this validator if it needs an existing field
    public function fieldNeedsToExist(): bool
    {
        return false;
    }

    // The unique key for this validator
    public function getKey(): string
    {
        return '

declare(strict_types=1);

namespace PHPValidation\Functions;

use PHPValidation\Fields\FieldValidatorInterface;
use PHPValidation\Fields\RequiredField;

function 

use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;

$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);

$builder->setValidators([
    'field1' => [new RequiredField(), new CustomValidator()],
]);

$validator = $builder->build();

use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;

use function PHPValidation\Functions\]);

$validator = $builder->build();

$builder->setValidators([
    'field' => [

$builder->setValidators([
    'field' => [notEmpty()],
]);

$builder->setValidators([
    'field' => [isArray()],
]);

$builder->setValidators([
    'field' => [hasKeys('key1', 'key2', 'key3')],
]);

$builder->setValidators([
    'field' => [hasValues(['value1', 'value2', 'value3'])],
]);

[
    'key1' => [
        'nestedKey1' => // YOUR VALUE HERE...
    ],
]

$builder->setValidators([
    'field1' => [confirmValues('field2')], // Targets the key on the first array level
    'field2' => [confirmValues('field2.nestedField1')], // Targets the key on the second array level
]);

$builder->setValidators([
    'field' => [in(['option1', 'option2', 'option3'])],
]);

$builder->setValidators([
    'field' => [notIn(['option1', 'option2', 'option3'])],
]);

$builder->setValidators([
    'field' => [minLength(5)],
]);

$builder->setValidators([
    'field' => [maxLength(10)],
]);

$builder->setValidators([
    'field' => [minCount(2)],
]);

$builder->setValidators([
    'field' => [maxCount(4)],
]);

$builder->setValidators([
    'field' => [email()],
]);

$builder->setValidators([
    'field' => [phoneNumber()],
]);

$builder->setValidators([
    'field' => [isAlphabetic()],
]);

$builder->setValidators([
    'field' => [isNumeric()],
]);

$builder->setValidators([
    'normalField' => [isAlphaNumeric()],
    'extraField' => [isAlphaNumeric(['.', ',', '\\', '[', ']'])], // Whitelists each individual character within the array
]);

$builder->setValidators([
    'field' => [isInt()],
]);

$builder->setValidators([
    'field' => [isFloat()],
]);

$builder->setValidators([
    'field' => [isString()],
]);

$builder->setValidators([
    'field' => [isObject()],
]);

$builder->setValidators([
    'field' => [objectOfType(DateTime::clas)],
]);

$builder->setValidators([
    'nonStrictField' => [equals('non-strict', false)],
    'strictField' => [equals('strict', true)],
]);

$builder->setValidators([
    'field' => [contains('substring')],
]);

$builder->setValidators([
    'field' => [greaterThan(30)],
]);

$builder->setValidators([
    'field' => [greaterEqual(30)],
]);

$builder->setValidators([
    'field' => [lowerThan(30)],
]);

$builder->setValidators([
    'field' => [lowerEqual(30)],
]);

$builder->setValidators([
    'field' => [between(0, 100)],
]);

$builder->setValidators([
    'field' => [isDate()],
]);

$builder->setValidators([
    'field' => [dateHasFormat('Y-m-d')],
]);

$builder->setValidators([
    'field' => [dateEquals(DateTime::createFromFormat('Y-m-d', '2000-12-31'), 'Y-m-d')],
]);

$builder->setValidators([
    'field' => [dateLowerThan(DateTime::createFromFormat('Y-m-d', '2000-12-31'), 'Y-m-d')],
]);

$builder->setValidators([
    'field' => [dateLowerEqual(DateTime::createFromFormat('Y-m-d', '2000-12-31'), 'Y-m-d')],
]);

$builder->setValidators([
    'field' => [dateGreaterThan(DateTime::createFromFormat('Y-m-d', '2000-12-31'), 'Y-m-d')],
]);

$builder->setValidators([
    'field' => [dateGreaterEqual(DateTime::createFromFormat('Y-m-d', '2000-12-31'), 'Y-m-d')],
]);

$dateMin = DateTime::createFromFormat('Y-m-d', '2000-12-29');
$dateMax = DateTime::createFromFormat('Y-m-d', '2000-12-30');

$builder->setValidators([
    'field' => [dateBetween($dateMin, $dateMax, 'Y-m-d')],
]);
bash
$ composer