Download the PHP package experience/validation without Composer
On this page you can find all versions of the php package experience/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download experience/validation
More information about experience/validation
Files in experience/validation
Package validation
Short Description Easy data validation, with support for separate 'create' and 'update' rulesets.
License MIT
Informations about the package validation
Overview
This package makes it easy to implement custom validation for any given array of data in your application. It also solves a common problem whereby the validation rules for a "create" action differ to those for an "update" action.
For example, let's say we need to validate some "user account" data, both at the point of registration, and if the user chooses to modify their account at a later date. In Laravel, our (partial) validation rules might look something like this:
That is, the value of the username
field must be unique within the users
table.
This is fine for account creation, but if the user updates his account without changing his username, the above validation rules will fail. The current users
table already contains the given username, and the validation rules don't care that it belongs to the user being validated.
In Laravel, we deal with this problem by telling the validator to ignore the id
of the current user, so our rules now look like this:
The Validation package solves this problem by separating the "create" and "update" validation rules, and allowing you to use a {key}
placeholder in your rules. Continuing with the above example, our validation rules now look like this:
More detailed implementation examples are provided in the "Usage" section, below.
Installation
Install the package via Composer, as follows:
If you're using Laravel, add the service provider to the providers
array in your app/config/app.php
file, as follows:
Usage
Let's assume you need to validate a registration form. First, create a custom "validator" class containing the necessary "create" and "update" rules. For example:
Next, inject an instance of your custom validator class into your controller, or wherever you perform your validation:
If validation passes, the validate
method returns true
. If validation fails, the validate
method throws a ValidationException
exception. You can catch the exception in your controller (as in the above example), or handle it globally in global.php
if you prefer.
Create a dedicated class for each set of data you wish to validate. For example, if your newly-registered users can log in to your site, you'll probably want a SessionValidator
:
As before, inject an instance of this validator class into your controller or service, and call the appropriate "validate" method:
Credits
This package was heavily influenced by the Laracasts Validation package. Whilst all the code was written from scratch, I owe a considerable debt of gratitude to Jeffrey Way for the general approach, and for his excellent tutorials at Laracasts.
Laracasts is hands-down the best learning resource available to PHP programmers (not just Laravel enthusiasts). If you're not already a subscriber, you should be.