Giter Site home page Giter Site logo

validator-service's Introduction

Validator Service for Laravel 4


Simple, yet powered with features validator service to validate your data.


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.

Laravel service provider

When using it with laravel4 you may want to add these lines to your config file:

ServiceProvider array

'Krucas\Service\Validator\ValidatorServiceProvider'

and Alias array

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

Now you can use power of facades.

Events

Validator service uses events to let other components to know that validator is doing some checks.

Events just after validator instance is being created:

  • service.validator.creating
  • service.validator.creating: Vendor\Package\Class

Events just after validator instance was created:

  • service.validator.created
  • service.validator.created: Vendor\Package\Class

Events before actual validation is started:

  • service.validator.validating
  • service.validator.validating: Vendor\Package\Class

Events after validation:

  • service.validator.validated
  • service.validator.validated: Vendor\Package\Class

Lets overview them quickly.

service.validator.creating

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

service.validator.creating: Vendor\Package\Class

Same as above just with a certain class name.

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.

service.validator.created: Vendor\Package\Class

Same as above just with a certain class name.

service.validator.validating

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

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.

service.validator.validated

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

service.validator.validated Vendor\Package\Class

Almost same as above, but with a class name.


All events are passing a Krucas\Service\Validator\Validator object instance to manipulate it.

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.

Eloquent sample model:

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.

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

Advanced usage with event listeners

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

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.

Lets define our interface for a routable models.

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.

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.

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.