2017 © Pedro Peláez
 

library validation

PHP Validation Library

image

phpbook/validation

PHP Validation Library

  • Friday, July 27, 2018
  • by phpbook
  • Repository
  • 0 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

About Validation

  • A PHP library for data validations
  • Requires PHP Extension FINFO.

Composer Install

composer require phpbook/validation

Declare Configurations


Declare Validation

layout = new \PHPBook\Validation\Layout;

        $this->layout->setAttribute('id', [
            'label' => 'Id',
            'type' => '@string',
            'uuid' => true,
            'required' => true,
        ])
        
        ->setAttribute('age', [
            'label' => 'Age',
            'type' => '@integer',
            'required' => true,
            'min' => 18,
            'max' => 100
        ])

        ->setAttribute('document', [
            'label' => 'Document',
            'type' => '@digits',
            'required' => true,
            'maxlength' => 15
        ])

        ->setAttribute('weight', [
            'label' => 'Weight',
            'type' => '@float',
            'required' => false
        ])

        ->setAttribute('name', [
            'label' => 'Name',
            'type' => '@string',
            'required' => true,
            'minlength' => 5,
            'maxlength' => 120
        ])

        ->setAttribute('photo', [
            'label' => 'Photo',
            'type' => '@file-buffer',
            'required' => true,
            'maxkbs' => 100,
            'mimes' => ['image' => 'Image File']
        ])
        
        ->setAttribute('status', [
            'label' => 'Status',
            'type' => '@string',
            'required' => false,
            'options' => [static::$STATUS_ACTIVE => 'Active', static::$STATUS_INACTIVE => 'Inactive', static::$STATUS_PROCESS => 'Process']
        ])
        
        ->setAttribute('friend',  [
            'label' => 'Friend',
            'type' => '\App\Module\Friend\Entity',
            'hints' => 'Friends',
            'required' => false
        ])

        ->setAttribute('friends', [
            'label' => 'Friends',
            'type' => '@array:\App\Module\Friend\Entity',
            'hints' => 'List of Friends'
        ])

        ->setAttribute('numbers', [
            'label' => 'Numbers',
            'type' => '@array:@integer',
        ])

        ->setAttribute('active', [
            'label' => 'Ativo',
            'type' => '@boolean',
            'required' => true
        ])

        ->setAttribute('date', [
            'label' => 'Date',
            'type' => '@date',
            'required' => true,
        ])

        ->setAttribute('datetime', [
            'label' => 'Date Time',
            'type' => '@datetime',
            'required' => true
        ])

        ->setAttribute('time', [
            'label' => 'Time',
            'type' => '@time',
            'required' => true
        ])

        ->setRule('ageOfJhon', ['name', 'age'], function($name, $age) {
            
            /* rules validations are called after the attributes validation */
            if (($name) and ($name == 'jhon')) {
                if (($age) and ($age setRule('anaCantBeHere', ['name'], function($name) {

            /* rules validations are called after the attributes validation */
            if ($name == 'ana') {
                /* you should throw exception like the basic validation does */
                throw new Exception('What are you doing here Ana?');
            };

        })

        ->setRule('statuRequiredForJhon', ['name', 'status'], function($name, $status) {

            /* rules validations are called after the attributes validation */
            if ($name == 'jhon') {
                if (!$status) {
                    /* you should throw exception like the basic validation does */
                    throw new Exception('Status is required for Jhon');
                };
            };

        });

    }

    public function getLayout(): \PHPBook\Validation\Layout {

        return $this->layout;

    }

}

?>

Use Validation

id, $this->name, $this->age) = $customerValidation->getLayout()->validate([null, $name, $age], ['id', 'name', 'age']);

        } catch(\Exception $e) {

            echo $e->getMessage();

        };

    }

    public function getName() {
        return $this->name;
    }

    public function getAge() {
        return $this->age;
    }

}

/* You can iterate the attributes */

$customerValidation = new CustomerValidation;

$attributes = $customerValidation->getLayout()->getAttributes();

foreach($attributes as $name => $attribute) {

    $name; //age

    $attribute; //['type' => '@integer', 'required' => true]
    
};


/* You can iterate and verbose the attributes */

$customerValidation = new CustomerValidation;

$attributes = $customerValidation->getLayout()->getAttributes();

foreach($attributes as $name => $attribute) {

    $customerValidation->getLayout()->getAttributeVerbose($name); //name must be a string. name must have max 120 characters. name is required
    
};

/* You can iterate the rules */

$customerValidation = new CustomerValidation;

$rules = $customerValidation->getLayout()->getRules();

foreach($rules as $key => $rule) {

    list($name, $attributes, $closure) = $rule;
    
};

/* You can get the rules verbose */

$customerValidation->getLayout()->getRulesVerbose(); //one string with all the rules descriptions



?>
  • Validation of date, datetime and time follows the international string format YYYY-MM-DD H:i:s | YYYY-MM-DD | H:i:s
  • Validation of date, datetime and time enable using DateTime class as value to be validated

The Versions

27/07 2018

dev-master

9999999-dev https://github.com/phpbook-sources/validation

PHP Validation Library

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Avatar phpbook

php validation lightweight fast

27/07 2018

1.0.2

1.0.2.0 https://github.com/phpbook-sources/validation

PHP Validation Library

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Avatar phpbook

php validation lightweight fast

26/07 2018

1.0.1

1.0.1.0 https://github.com/phpbook-sources/validation

PHP Validation Library

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Avatar phpbook

php validation lightweight fast

02/07 2018

1.0.0

1.0.0.0 https://github.com/phpbook-sources/validation

PHP Validation Library

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

by Avatar phpbook

php validation lightweight fast