Download the PHP package gerob/l4-validation without Composer
On this page you can find all versions of the php package gerob/l4-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package l4-validation
Laravel Extended Validator
Introduction
This package was created for several purposes:
- To ease the creation of validation services - we all know validaton should be moved out of the controller
- Provide an easy way to provide context for validations - such as providing a modified set of validation rules for edit vs. create
- Provide an easy way to group validations - that is, calling one 'passes()' method to validate multiple models
- Fork to allow updating framework
Installation
Install this package through Composer. Add the following to your composer.json file:
Next, run composer install
Finally, add the service provider and the facade to app/config/app.php.
Validation as a Service
Most Basic Validator
To create a validation service simply extend the base abstract ContextualValidation class and provide an array of rules and messages (messages will override Laravel's default validation messages).
This service is then instantiated and the validation works much the same as Laravel's built-in validation.
Contextual Validation
Providing Contexts
Sometimes you need to have different rules for the same model depending on the context you are using it within. For instance, maybe some fields are mandatory during registration, but may not be when the user is editing their profile.
We can turn our rules array into a multidimensional array, with unique keys for each of our contexts. The default key denotes our default rules, which will be used for every validation. Default rules can be overridden very easily by setting the rule in other contexts.
Let's see how we can use one of the contexts during the creation of a user:
While editing a user, we would do this:
Multiple contexts can be added as well:
Providing Data to Contexts
Sometimes you need to provide data to your rules. For instance, you may have a unique rule on a field, but when editing the record you don't want an error to be thrown when the user submits a form with the value they previously had.
Luckily, we have a way to handle these instances.
Notice that we have set the email field to be unique by default. However, when the users submits an edit form we want to ignore the current user's id. We use the @id placeholder, and then replace that with the desired value:
The method bindReplacement() takes the rule name as the first parameter; the second parameter is an array of bindings, with the key being the placeholder and value being the replacement value.
Grouped Validator
Have you ever had a form submit data that required two different models be created or updated? Validating both models can be a pain, but that's where the GroupedValidator class comes in.
Let's say we posted a form that is going to create a new user and their car; we therefore need to validate both a user and a car model.
We can use validator contexts with grouped validation as well:
We can also mix and match ContextualValidator services and native Laravel Validators:
Adding Custom Complex Validation
Adding complex validation to your validation services is as easy as follows:
All we do is add the method to our validation service, which accepts an instance of , allowing us to utilize Laravel's built-in support for complex validations.
All versions of l4-validation with dependencies
illuminate/support Version 4.1.*
illuminate/http Version 4.1.*
illuminate/validation Version 4.1.*