Package Data | |
---|---|
Maintainer Username: | edvinaskrucas |
Maintainer Contact: | edv.krucas@gmail.com (Edvinas Kručas) |
Package Create Date: | 2013-04-15 |
Package Last Update: | 2013-04-20 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-14 15:09:48 |
Package Statistics | |
---|---|
Total Downloads: | 79 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 0 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Simple, yet powered with features validator service to validate your data.
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.
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.
Validator service uses events to let other components to know that validator is doing some checks.
Events just after validator instance is being created:
Events just after validator instance was created:
Events before actual validation is started:
Events after validation:
Lets overview them quickly.
This is triggered before rules + attributes assigned. If some listener returns false
then validation will return false automatically without validating.
Same as above just with a certain class name.
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.
Same as above just with a certain class name.
This event is fired first, and if some listener returned false
then it will cancel validating and return false
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.
Event is fired just when validation returned true
, this event wont stop any further actions.
Almost same as above, but with a class name.
All events are passing a Krucas\Service\Validator\Validator
object instance to manipulate it.
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.
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.