Coder Social home page Coder Social logo

rulez-laravel's Introduction

Rulez - Validation for Laravel

Build Status ProjectStatus

Rulez provides easy way for setting up input validation rules and validation service. You can add all your rules from one place and later use a facade to validate input.

Rulez can set sepparate rule for create and update methods. Handy when there's a unique field.

Installation

Require keevitaja/rulez with composer

composer require keevitaja/rulez:dev-master

Add service provider and alias to app/config/app.php

'providers' => array(

    'Keevitaja\Rulez\RulezServiceProvider',

),
'aliases' => array(

    'Rulez'           => 'Keevitaja\Rulez\RulezFacade'

),

Usage

Validation rules can be set up in various places, like routes.php, but probably best way would be to create app/rules.php file and require it in the app/start/global.php.

require app_path().'/rules.php';

Setting the input validation rules

Rules can be set sepparately for creation and update. Base rules will apply for both. In the example below, users is the name for the rule set, which can be used later in the controller.

Rulez::register('users', function($rulez)
{
    $rulez->addBase([
        'first_name' => 'required|min:2',
        'last_name' => 'required|min:2',
        'password' => 'required|min:6'
    ]);

    $rulez->addCreate([
        'email' => 'required|unique:users|email'
    ]);

    $rulez->addUpdate([
        'email' => 'required|unique:users,email,%s|email'
    ]);
});

If you do not need sepparate rules for create and update, then just use base rules.

Validating the input

Rulez::validateBase($name, $input)

Validates base rules.

Rulez::validateCreate($name, $input)

Merges create and base rules and validates.

Rulez::validateUpdate($name, $input, $exclude = false)

Merges update and base rules, sets the row id for case there's a unique column and validates.

'users' is the name used with the rule registration in app/rules.php.

See the example below:

$input = Input::all();

if (Rulez::validateUpdate('users', $input, $id))
{
    // all ok, lets do the update and redirect
}

// something does not validate, send user back with errors and input

return Redirect::back()->withErrors(Rulez::validationErrors())->withInput();

If you like this

please follow me @keevitaja

rulez-laravel's People

Contributors

keevitaja avatar

Watchers

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