keevitaja/rulez

This package is abandoned and no longer maintained. No replacement package was suggested.

User input validation

dev-master 2014-03-03 11:50 UTC

This package is auto-updated.

Last update: 2022-02-01 12:31:54 UTC


README

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