Skip to content

edvinaskrucas/validator-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Validator service for laravel4, for easier validations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages