Skip to content

jartaud/laravel-validator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Validation Service

Total Downloads Latest Stable Version Latest Unstable Version License

Installation

Add "prettus/laravel-repository": "dev-master" to composer.json

"prettus/laravel-validation": "dev-master"

Create a validator

The Validator contains rules for adding, editing.

Prettus\Validator\Contracts\ValidatorInterface::RULE_CREATE
Prettus\Validator\Contracts\ValidatorInterface::RULE_UPDATE

In the example below, we define some rules for both creation and edition

<?php

use \Prettus\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {

    protected $rules = array(
        'title' => 'required',
        'text'  => 'min:3',
        'author'=> 'required'
    );

}

To define specific rules, proceed as shown below:

<?php

use \Prettus\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {

    protected $rules = array(
        ValidatorInterface::RULE_CREATE=>array(
            'title' => 'required',
            'text'  => 'min:3',
            'author'=> 'required'
        ),
        ValidatorInterface::RULE_UPDATE=>array(
            'title' => 'required'
        )
    )

}

Using the Validator

repository = $repository; $this->validator = $validator; } public function store() { try { $this->validator->with( Input::all() )->passesOrFail(); // OR $this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_CREATE ); $post = $this->repository->create( Input::all() ); return Response::json(array( 'message'=>'Post created', 'data' =>$post->toArray() )); } catch (ValidatorException $e) { return Response::json(array( 'error' =>true, 'message' =>$e->getMessage() )); } } public function update($id) { try{ $this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_UPDATE ); $post = $this->repository->update( Input::all(), $id ); return Response::json(array( 'message'=>'Post created', 'data' =>$post->toArray() )); }catch (ValidatorException $e){ return Response::json(array( 'error' =>true, 'message' =>$e->getMessage() )); } } } # Author Anderson Andrade - ## Credits http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%