PHP code example of the-support-group / validator

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

    

the-support-group / validator example snippets




use Respect\Validation\Validator as RespectValidator;
use TheSupportGroup\Common\Validator\Helpers;
use TheSupportGroup\Common\ValidationAdaptor\ValidationAdaptor;

// Prep up the validator, ideally done using DI.
$respectValidator = new RespectValidator();
$errorBag = new Helpers\FieldsErrorBag();

// Note that any library can be used as long as it is accepted by the ValidationAdaptor.
$validationProvider = new ValidationAdaptor($respectValidator);
$validationResultProcessor = new Helpers\ValidationResultProcessor($errorBag);
$rulesFactory = new Helpers\RulesFactory();

// Create the validation facade that will give us our validation object to work with.
$validationFacade = new ValidatorFacade(
    $validationProvider,
    $validationResultProcessor,
    $rulesFactory
);




// Prepare the input data.
$inputData = $_POST;

// Prepare rules to be applied on the input data.
$rules = [
    # firstname and lastname must exists
    # they should be alphanumeric
    # atleast 2 characters
    'firstname, lastname' => 'ers' table
    'id'                  => 'numeric|exists:users',
    'age'                 => 'min:16|numeric',
    'info[country]'       => '8(123)456-12-34
    # wrong: +38(123)56-123-56
    # wrong: +39(123)456-12-34
    'phoneMask'           => 'phoneMask:(+38(###)###-##-##)',
    'randNum'             => 'between:1, 10|numeric',

    # the value must be an IP Format
    'ip'                  => 'ip',
    'password'            => '        => 'Email has bad format',
   'email.unique'        => 'This email :value: is not unique',
   'elevatorFloor.notIn' => 'Oops',
];

// Run validation on input data.
$validationResult = $validationFacade->validate($inputData, $rules, $customMessages);


// Check if there are any errors.
$validationResult->hasErrors();

// Count number of errors.
$validationResult->count();

// Get all errors.
$validationResult->getErrors();

// Get error for a specific field.
$validationResult->getErrors('username');

// Get raw error messages with keys.
$validationResult->getRawErrors();

// Get only the first error message for each field.
$validationResult->firsts();

// Get the first error message for a specific field.
$validationResult->first('username');

// Appending an error message.
$validationResult->fieldsErrorBag->add($fieldName, $message);