Code Monkey home page Code Monkey logo

validation's Introduction

PHP VALIDATION TRAIT

Programming language Scrutinizer code quality Travis build

This is a simple PHP standalone trait, that can be used to validate properties in any object. The validation process based on the Symfony Validation Component

๐Ÿš€ Installation

This library requeired PHP version 7.2 or highter. And composer - package manger for PHP.

  $ composer require b-tokman/validation 

๐Ÿ’ก Usage

After the installation you'll be able to use the bTokman\validation\ValidationTrait trait in your app.

You cannot override a trait's property in the class where the trait is used. However, you can override a trait's property in a class that extends the class where the trait is used:

  • Start using trait in your base class
  • Declare validation rules in your extended class. List of availible Rules
  • On a new instance of your class just call method validate.
class BaseObject
{
    use bTokman\validation\ValidationTrait;
}

......

class ValidationObject extends BaseObject
{
    public $validationRules = [
        'password' => [NotBlank::class, [Length::class, ['min' => 8]]],
    ];

    public $password;
}

......

$object = new ValidationObject();
   
$result = $object->validate();

Or you can override a trait's property in a class constructor:

class ValidationObject
{
    use bTokman\validation\ValidationTrait;

    public function __construct()
    {
        $this->validationRules = [
            'password' => [NotBlank::class, [Length::class, ['min' => 8]]],
        ];
    }
    
    public $password;
}

$object = new ValidationObject();
   
$result = $object->validate();

The validation result is

  • array of errors [[fieldName] => [errorMessage1, errorMessag2, ...] .
  • null if the validation was passed.

validation's People

Contributors

btokman avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.