2017 © Pedro Peláez
 

library plugin-validation

PHP Composer Plugin for Validation

image

harryosmar/plugin-validation

PHP Composer Plugin for Validation

  • Wednesday, April 4, 2018
  • by harryosmar
  • Repository
  • 2 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 20 Versions
  • 0 % Grown

The README.md

Validation

Composer plugin for validation purpose, contains set of validation rules., (*1)

Latest Version Build Status Scrutinizer Code Quality Code Coverage, (*2)

Features

validation > fields > rules

validation structure, (*3)

Installation

Add this composer.json file, (*4)

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

Then running, (*5)

$ composer install

Or type, (*6)

composer require harryosmar/plugin-validation

in composer.json root directory, (*7)

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, (*8)

<?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, (*9)

<?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, (*10)

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

2 Types Validation

Validation

see the sample code, (*11)

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, (*12)

<?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, (*13)

<?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, (*14)

<?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, (*15)

<?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, (*16)

<?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., (*17)

The Versions

04/04 2018

dev-target2

dev-target2 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

04/04 2018

dev-target1

dev-target1 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

08/03 2018

dev-master

9999999-dev https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

08/03 2018
08/03 2018

dev-target

dev-target https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

08/03 2018

dev-form-validation

dev-form-validation https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

27/02 2018

v2.2.2

2.2.2.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

27/02 2018

dev-field-multi-value

dev-field-multi-value https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

27/02 2018

v2.2.1

2.2.1.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

26/02 2018

v2.2.0

2.2.0.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

26/02 2018

v2.1.5

2.1.5.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

25/02 2018

v2.1.4

2.1.4.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

21/02 2018

v2.1.3

2.1.3.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

 

The Development Requires

validation form validation validaton rules

18/02 2018

v2.1.2

2.1.2.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

validation form validation validaton rules

17/02 2018

v2.1.1

2.1.1.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

validation form validation validaton rules

17/02 2018

v2.1.0

2.1.0.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

validation form validation validaton rules

15/02 2018

v2.0.0

2.0.0.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires

validation form validation validaton rules

11/07 2016

v1.1.1

1.1.1.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

validation form validation validaton rules

11/07 2016

v1.1.0

1.1.0.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

validation form validation validaton rules

06/07 2016

v1.0.0

1.0.0.0 https://github.com/harryosmar/plugin-validation

PHP Composer Plugin for Validation

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

validation form validation validaton rules