harryosmar/plugin-validation

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.
There is no license information available for the latest version (v2.2.3) of this package.

PHP Composer Plugin for Validation

v2.2.3 2018-03-08 10:55 UTC

This package is not auto-updated.

Last update: 2021-05-05 16:47:48 UTC


README

Composer plugin for validation purpose, contains set of validation rules.

Latest Version Build Status Scrutinizer Code Quality Code Coverage

Features

validation > fields > rules

validation structure

Installation

Add this composer.json file

{
    "require": {
        "harryosmar/plugin-validation": "^2.2"
    }
}

Then running

$ composer install

Or type

composer require harryosmar/plugin-validation

in composer.json root directory

How To Use

1. initialize
<?php
use PluginSimpleValidate\Validation;
use PluginSimpleValidate\Field;
use PluginSimpleValidate\Libraries\Language;

$language = new Language('en');
$validation = new Validation($this->language);
$emailField = (new Field('email', ''))->required()->validEmail();
$passwordField = (new Field('password', ''))->required()->isAlphaOrNumeric()->lengthGreaterThan(5); // add chain of rules to the `field`
2. add fields to $validation object
<?php
/**
 * @var \PluginSimpleValidate\Validation $validation
 * @var \PluginSimpleValidate\Field $emailField
 * @var \PluginSimpleValidate\Field $passwordField
 */
$validation->addField($emailField)->addField($passwordField); // add chain of fields to `$validation` object
3. call the $validation run method
<?php 
/** @var \PluginSimpleValidate\Validation $validation */
$status = $validation->run();
4. get the errors message if $status is false
<?php
/** 
 * @var \PluginSimpleValidate\Validation $validation 
 * @var bool $status
 */
if (!$status) {
    $errors = $validation->getErrors(); // return array of errors message
}

$erros values

<?php
[
    'email' => [
        'field is required',
        'field must be a valid email address'
    ],
    'password' => [
        'field is required',
        'field may only letters and numbers',
        'field length must be greater than 5'
    ],
];
validation with option break validation chain

You can break the validation chain if there is a field get an error. In step 3, when calling the method run of $validation object, add parameter true to enable break the chain when error occured

<?php 
/**
 * @var \PluginSimpleValidate\Validation $validation
 * @var bool $status
 */
$status = $validation->run(true);

if (!$status) {
    $errors = $validation->getErrors(); // return array of errors message
}

then $erros values will be

<?php
[
    'email' => [
        'field is required',
        'field must be a valid email address'
    ]
];

2 Types Validation

Validation

see the sample code

Form Validation

<?php
use PluginSimpleValidate\FormValidation;

/** @var \PluginSimpleValidate\Libraries\Language $language */
$validation = new FormValidation($language);

/**
* sample of request body
 * [
       'email' => 'abc',
       'grant_type' => '',
       'client_id' => '',
       'client_secret' => '',
       'redirect_uri' => '',
       'username' => '',
       'password' => '',
       'scope' => '',
   ]
*/


$validation->createFromPost(
    \Zend\Diactoros\ServerRequestFactory::fromGlobals(),
    [
        'email' => 'required,validEmail',
        'grant_type' => 'required',
        'client_id' => 'required,isNumber',
        'client_secret' => 'required,isAlphaOrNumeric',
        'redirect_uri' => 'required',
        'username' => 'required,lengthBetweenOrEqual:5:10',
        'password' => 'required,lengthGreaterThan:5',
        'scope' => 'required',
    ]
);

$validation->run();

if (!$validation->getStatus()) {
    $errors = $validation->getErrors();
}

The $erros will be

<?php
[
    'email' => [
        'field must be a valid email address',
    ],
    'grant_type' => [
        'field is required',
    ],
    'client_id' => [
        'field is required',
        'field must be a number',
    ],
    'client_secret' => [
        'field is required',
        'field may only letters and numbers',
    ],
    'redirect_uri' => [
        'field is required',
    ],
    'username' => [
        'field is required',
        'field length must be greater or equal than 5 or less or equal than 10',
    ],
    'password' => [
        'field is required',
        'field length must be greater than 5',
    ],
    'scope' => [
        'field is required',
    ],
];

Field

There are 2 type of field available :

  • Field with single value PluginSimpleValidate\Field
<?php
use PluginSimpleValidate\Field;
use PluginSimpleValidate\Validation;

$firstNameField = (new Field('firstname', ''))->required()->lengthGreaterOrEqualThan(4);
$lastNameField = (new Field('lastname', ''))->required()->lengthGreaterOrEqualThan(4);
$fullNameField = (new Field(
    'fullname',
    $firstNameField->getValue() . ' ' . $lastNameField->getValue()
))->lengthGreaterOrEqualThan(10);

/** @var Validation $validation  */
$validation->addField($firstNameField)->addField($lastNameField)->addField($fullNameField);

if (!$validation->run()) {
    $errors = $validation->getErrors();
}

The $errors will be

<?php
[
    [
        'firstname' => [
            'field is required',
            'field length must be greater or equal than 4',
        ],
        'lastname' => [
            'field is required',
            'field length must be greater or equal than 4',
        ],
        'fullname' => [
            'field length must be greater or equal than 10',
        ],
    ]
];
  • Field with multi values PluginSimpleValidate\MultiValues\Field
<?php
use PluginSimpleValidate\MultiValues\Field;
use PluginSimpleValidate\Validation;

$firstName = '';
$lastName = '';

/** @var Validation $validation  */
$validation->addField((new Field('name'))
    ->isTrue($firstName !== '', 'first name required')->isTrue(strlen($firstName) >= 4, 'first name length must be at least 4')
    ->isTrue($lastName !== '', 'last name required')->isTrue(strlen($lastName) >= 4, 'last name length must be at least 4')
    ->isTrue(strlen($firstName . ' ' . $lastName) >= 10, 'full name length must be at least 10'));

if (!$validation->run()) {
    $errors = $validation->getErrors();   
}

The $errors will be

<?php
[
    'name' => [
        'first name required',
        'first name length must be at least 4',
        'last name required',
        'last name length must be at least 4',
        'full name length must be at least 10',
    ]
];

Available Rules

required
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->required();
numeric
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isNumber();
email
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->validEmail();
aplha
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isAlpha();
alpha or numeric
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isAlphaOrNumeric();
decimal
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isDecimal();
natural
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isNatural();
natural with no zero
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isNaturalNoZero();
equal
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->equal('old password');
less than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lessThan(5);
greater than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->greaterThan(5);
less or equal than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lessOrEqualThan(5);
greater or equal than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->greaterOrEqualThan(5);
between
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->between(5, 10);
between or equal
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->betweenOrEqual(5, 10);
length
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->length(5);
length less than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthLessThan(5);
length greater than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthGreaterThan(5);
length less or equal than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthLessOrEqualThan(5);
length greater or equal than
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthGreaterOrEqualThan(5);
length between
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthBetween(5, 10);
length between or equal
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->lengthBetweenOrEqual(5, 10);
is true for custom rule logic
<?php
$field = new \PluginSimpleValidate\Field('field', someMethod());
$field->isTrue('this is for error message');

function someMethod() : bool 
{
    // add logic here
    return true;
}
valid phone
<?php
/**
 * @var \PluginSimpleValidate\Field $field
 */
$field->isValidPhone('ID'); // `ID` is region code for `indonesia` country

Custom translation

If you want to use your own translation, when instantiate Language class provide the parameter $translation array

<?php
use PluginSimpleValidate\Libraries\Language;

/** 
 * @var array $translationArray
 * $translationArray must have the same format with https://github.com/harryosmar/plugin-validation/blob/master/src/lang/en.php
 */
$language = new Language('pl', $translationArray);

Submitting bugs and feature requests

Harry Osmar Sitohang - harryosmarsitohang@gmail.com - https://github.com/harryosmar
See also the list of contributors which participated in this project.