2017 © Pedro Peláez
 

library validator-service

Validator service for a Laravel4

image

edvinaskrucas/validator-service

Validator service for a Laravel4

  • Saturday, April 20, 2013
  • by edvinaskrucas
  • Repository
  • 0 Watchers
  • 4 Stars
  • 73 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Validator Service for Laravel 4


Simple, yet powered with features validator service to validate your data., (*1)


Installation

As it is using composer packaging system you need just to add this "edvinaskrucas/validator-service": "dev-master"" to your composer.json file and update your project., (*2)

Laravel service provider

When using it with laravel4 you may want to add these lines to your config file:, (*3)

ServiceProvider array, (*4)

'Krucas\Service\Validator\ValidatorServiceProvider'

and Alias array, (*5)

'ValidatorService' => 'Krucas\Service\Validator\Facades\ValidatorService'

Now you can use power of facades., (*6)

Events

Validator service uses events to let other components to know that validator is doing some checks., (*7)

Events just after validator instance is being created: * service.validator.creating * service.validator.creating: Vendor\Package\Class, (*8)

Events just after validator instance was created: * service.validator.created * service.validator.created: Vendor\Package\Class, (*9)

Events before actual validation is started: * service.validator.validating * service.validator.validating: Vendor\Package\Class, (*10)

Events after validation: * service.validator.validated * service.validator.validated: Vendor\Package\Class, (*11)

Lets overview them quickly., (*12)

service.validator.creating

This is triggered before rules + attributes assigned. If some listener returns false then validation will return false automatically without validating., (*13)

service.validator.creating: Vendor\Package\Class

Same as above just with a certain class name., (*14)

service.validator.created

This is triggered just after new instance was created and rules + attributes assigned. If some listener returns false then validation will return false automatically without validating., (*15)

service.validator.created: Vendor\Package\Class

Same as above just with a certain class name., (*16)

service.validator.validating

This event is fired first, and if some listener returned false then it will cancel validating and return false, (*17)

service.validator.validating: Vendor\Package\Class

Event is almost the same is previous one, expect this lets you to listen to a certain class to be validated. Where Vendor\Package\Class validated class name will be placed. If some listeners returned false, then validation method will be canceled., (*18)

service.validator.validated

Event is fired just when validation returned true, this event wont stop any further actions., (*19)

service.validator.validated Vendor\Package\Class

Almost same as above, but with a class name., (*20)


All events are passing a Krucas\Service\Validator\Validator object instance to manipulate it., (*21)

Usage

Basic usage

You can use it to validate your models, forms and other stuff, you just need to implement ValidatableInterface and you are ready., (*22)

Eloquent sample model:, (*23)

class Page extends Eloquent implements Krucas\Service\Validator\Contracts\ValidatableInterface
{
    public function getValidationRules()
    {
        return array(
            'title'     => 'required|max:255',
            'content'   => 'required'
        );
    }

    public function getValidationValues()
    {
        return $this->attributes;
    }
}

Now you are ready to validate it., (*24)

$page = new Page();

$validatorService = ValidatorService::make($page);

if($validatorService->passes())
{
    return 'OK';
}
else
{
    $errors = $validatorService->getErrors();
}

This example shows how easily you can set up your validation., (*25)

Advanced usage with event listeners

This example will show more advanced usage (I used this in my case)., (*26)

We have a package named Routing, basically what it does is just stores some URL's to a database and resolves objects from a polymorphic relations., (*27)

Lets define our interface for a routable models., (*28)

interface RoutableInterface
{
    public function getUri();
}

Now we need to handle all routable models, add additional checks when validating our data, we can do this very easy when listening for some events., (*29)

Event::listen('service.validator.validating', function(Validator $validatorService)
{
    // Check if our validatable object implements RoutableInterface
    // If it is, then add some extra rules and values for a validator
    if(in_array('RoutableInterface', class_implements($validatorService->getValidatable())))
    {
        $validatorService->setAttributeRules('uri', 'required|max:255|unique:uri,uri');
        $validatorService->setAttributeValue('uri', Input::get('uri'));
    }
});

Thats it, this will inject some extra rules and values for a every Routable model instance when it is validating. After success validation you can insert some records to your db., (*30)

The Versions

20/04 2013

dev-master

9999999-dev

Validator service for a Laravel4

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edvinas Kručas

laravel service validator