Giter Club home page Giter Club logo

laravel-validation-rule-builder's Introduction

Laravel Validation Rule Builder

Install

Just run the composer require command from the Terminal:

$ composer require ukeloop/laravel-validation-rule-builder

Validation Rule Builder

You can define validation rules with method chain pattern.

use Illuminate\Support\Facades\Validator;
use Ukeloop\ValidationRuleBuilder\ValidationRuleBuilder;

Validator::make($input, [
    'title' => (new ValidationRuleBuilder())->required()->string()->max(255),
]);

You can use Laravel validation rules.

use Ukeloop\ValidationRuleBuilder\ValidationRuleBuilder;

$validationRule = new ValidationRuleBuilder();

$validationRule->required();
$validationRule->sometime();
$validationRule->nullable();

$validationRule->string();
$validationRule->integer();
$validationRule->numeric();
$validationRule->date();
$validationRule->bool();

$validationRule->min(8);
$validationRule->max(255);

$validationRule->email('strict', 'dns');

// etc ...

For other validation rules, please refer to the laravel docs.

Using Rule Objects

$validationRule->add(Password::min(8));

Using Closures

new ValidationRuleBuilder(
    function ($attribute, $value, $fail) {
        if ($value === 'foo') {
            $fail('The ' . $attribute . ' is invalid.');
        }
    }
);

Validation Rule Sets

Validation rule sets are created by combining laravel validation rules. This will help you can keep your project's validation rules more DRY.

e.g. Rule of email address

use Ukeloop\ValidationRuleBuilder\RuleSet;

class EmailRuleSet extends RuleSet
{
    public static function rules(): ValidationRuleBuilder
    {
        return new ValidationRuleBuilder('email:strict,dns,spoof');
    }
}
use Illuminate\Support\Facades\Validator;

Validator::make($input, [
    'email' => EmailRuleSet::rules()->somtime()->required(),
]);

e.g. Rule of password

use Ukeloop\ValidationRuleBuilder\RuleSet;

class PasswordRuleSet extends RuleSet
{
    public static function rules(): ValidationRuleBuilder
    {
        return new ValidationRuleBuilder(
            Password::min(8)
                ->letters()
                ->mixedCase()
                ->numbers()
                ->symbols()
        );
    }
}
use Illuminate\Support\Facades\Validator;

Validator::make($input, [
    'password' => PasswordRuleSet::rules()->required()->confirmed(),
]);

Testing

$ composer test

laravel-validation-rule-builder's People

Contributors

ukeloop 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.