Remove Default Search Box and Add Custom Box in DataTable
April 22, 2017
How To Install LAMP Stack On Ubuntu 16.04
May 4, 2017
Show all

Valitron: Easy Validation

Valitron :

Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax. Valitron is the simple and pragmatic validation library you’ve been looking for.

Valitron was created out of frustration with other validation libraries that have dependencies on large components from other frameworks like Symfony’s HttpFoundation, pulling in a ton of extra files that aren’t really needed for basic validation. It also has purposefully simple syntax used to run all validations in one call instead of individually validating each value by instantiating new classes and validating values one at a time like some other validation libraries require.

In short, Valitron is everything you’ve been looking for in a validation library but haven’t been able to find until now: simple pragmatic syntax, lightweight code that makes sense, extensible for custom callbacks and validations, well tested, and without dependencies. Let’s get started.
Installation

php composer.phar require vlucas/valitron

Usage:

Usage is simple and straightforward. Just supply an array of data you wish to validate, add some rules, and then call validate(). If there are any errors, you can call errors() to get them.

Using this format, you can validate $_POST data directly and easily, and can even apply a rule like required to an array of fields:

You may use dot syntax to access members of multi-dimensional arrays, and an asterisk to validate each member of an array:

Setting language and language dir globally:

Built-in Validation Rules

required – Required field
equals – Field must match another field (email/password confirmation)
different – Field must be different than another field
accepted – Checkbox or Radio must be accepted (yes, on, 1, true)
numeric – Must be numeric
integer – Must be integer number
boolean – Must be boolean
array – Must be array
length – String must be certain length
lengthBetween – String must be between given lengths
lengthMin – String must be greater than given length
lengthMax – String must be less than given length
min – Minimum
max – Maximum
in – Performs in_array check on given array values
notIn – Negation of in rule (not in array of values)
ip – Valid IP address
email – Valid email address
url – Valid URL
urlActive – Valid URL with active DNS record
alpha – Alphabetic characters only
alphaNum – Alphabetic and numeric characters only
slug – URL slug characters (a-z, 0-9, -, _)
regex – Field matches given regex pattern
date – Field is a valid date
dateFormat – Field is a valid date in the given format
dateBefore – Field is a valid date and is before the given date
dateAfter – Field is a valid date and is after the given date
contains – Field is a string and contains the given string
creditCard – Field is a valid credit card number
instanceOf – Field contains an instance of the given class
optional – Value does not need to be included in data array. If it is however, it must pass validation.

NOTE: If you are comparing floating-point numbers with min/max validators, you should install the BCMath extension for greater accuracy and reliability. The extension is not required for Valitron to work, but Valitron will use it if available, and it is highly recommended.

Leave a Reply

Your email address will not be published. Required fields are marked *