2017 © Pedro Peláez
 

library validator

Form input validator

image

codezero/validator

Form input validator

  • Saturday, August 23, 2014
  • by codezero
  • Repository
  • 2 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Form Input Validator

Build Status Latest Stable Version Total Downloads License, (*1)

This package provides an easy to use interface to handle server side form validation and lets you create your custom form validation classes without much effort., (*2)

Although the core of this package is not bound to any framework, I have included a ServiceProvider and ValidationService implementation specifically for Laravel., (*3)

I have also included a ValidationTrait and a facade (both for Laravel) so you can use this package in a way you like best (see below for more)., (*4)

Installation

Install this package through Composer:, (*5)

"require": {
    "codezero/validator": "1.*"
}

Laravel 4 Implementation

After installing, update your app/config/app.php file to include a reference to this package's service provider in the providers array:, (*6)

'providers' => [
    'CodeZero\Validator\ValidatorServiceProvider'
]

This package will automatically register the Validate alias, if this is not already taken., (*7)

You can handle failed validations by catching the ValidationException. One automated way is to add the following handler to app/start/global.php. But you could as easily catch it in a try/catch block., (*8)

App::error(function(CodeZero\Validator\Exceptions\ValidationException $exception)
{
    return Redirect::back()->withInput()->withErrors($exception->getErrors());
});

Laravel Specific Usage

1. Create a FormValidator to validate your form

use CodeZero\Validator\FormValidator;

class UpdateUserForm extends FormValidator {

    /**
     * Validation rules
     *
     * @var array
     */ 
    protected $rules = [
        'name' => 'required',
        'email' => 'required|email|unique,email,{userId}'
    ];

}

Note the {userId} placeholder as an example., (*9)

2. Handle the input

Create your form and then handle the input in your controller in one of the following ways., (*10)

Validate with a facade:
$input = Input::all();
$vars = ['{userId}' => $someUser->id];

Validate::form('UpdateUserForm', $input, $vars);

You can pass the input and any placeholder key/value pairs as the second and third parameter, but if you leave both off, the package will automatically fetch Input::all() for you., (*11)

To have typesafety or autocomplete in IDE's, you might do this instead:, (*12)

Validate::form(get_class(UpdateUserForm), $input, $vars);

Or if you use PHP 5.5 or above, this is even cleaner:, (*13)

Validate::form(UpdateUserForm::class, $input, $vars);
Validate with a trait:
use CodeZero\Validator\Support\ValidatorTrait;

class HomeController extends BaseController {

    use ValidatorTrait;

    public function update()
    {
        $input = Input::all();
        $vars = ['{userId}' => $someUser->id];

        $this->validate(UpdateUserForm::class, $input, $vars);

        return Redirect::to('/');
    }

}

3. Show any validation errors

Showing the errors in your form is as easy as doing this for each field:, (*14)

{{ $errors->first('name', '<p>:message</p>'); }}

That's it!, (*15)

The Versions

23/08 2014

dev-master

9999999-dev

Form input validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel form validation input

23/08 2014

1.0.1

1.0.1.0

Form input validator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel form validation input

10/08 2014

1.0.0

1.0.0.0

Form input validator.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ivan Vermeyen

laravel form validation input