dev-master
9999999-devValidator service for a Laravel4
MIT
The Requires
The Development Requires
by Edvinas Kručas
laravel service validator
Wallogit.com
2017 © Pedro Peláez
Validator service for a Laravel4
Simple, yet powered with features validator service to validate your data., (*1)
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)
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)
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)
This is triggered before rules + attributes assigned. If some listener returns false then validation will return false automatically without validating., (*13)
Same as above just with a certain class name., (*14)
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)
Same as above just with a certain class name., (*16)
This event is fired first, and if some listener returned false then it will cancel validating and return false, (*17)
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)
Event is fired just when validation returned true, this event wont stop any further actions., (*19)
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)
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)
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)
Validator service for a Laravel4
MIT
laravel service validator