Download the PHP package s-bhojani/validation without Composer

On this page you can find all versions of the php package s-bhojani/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package validation

Respect\Validation

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

The most awesome validation engine ever created for PHP.

Installation

Packages available on PEAR and Composer. Autoloading is PSR-0 compatible.

Feature Guide

Namespace Import

Respect\Validation is namespaced, but you can make your life easier by importing a single class into your context:

Simple Validation

The Hello World validator is something like this:

Chained Validation

It is possible to use validators in a chain. Sample below validates a string containing numbers and letters, no whitespace and length between 1 and 15.

Validating Object Attributes

Given this simple object:

Is possible to validate its attributes in a single chain:

Validating array keys is also possible using v::key()

Note that we used v::string() and v::date() in the beginning of the validator. Although is not mandatory, it is a good practice to use the type of the validated object as the first node in the chain.

Input optional

All validators treat input as optional and will accept empty string input as valid, unless otherwise stated in the documentation.

We use the v:notEmpty() validator prefixed to disallow empty input and effectively define the field as mandatory as input will be required or validation will fail.

Negating Rules

You can use the v::not() to negate any rule:

Validator Reuse

Once created, you can reuse your validator anywhere. Remember $usernameValidator?

Informative Exceptions

When something goes wrong, Validation can tell you exactly what's going on. For this, we use the assert() method instead of validate():

The printed message is exactly this, as a text tree:

\-All of the 3 required rules must pass
  |-"really messed up screen#name" must contain only letters (a-z) and digits (0-9)
  |-"really messed up screen#name" must not contain whitespace
  \-"really messed up screen#name" must have a length between 1 and 15

Getting Messages

The text tree is fine, but unusable on a HTML form or something more custom. You can use findMessages() for that:

findMessages() returns an array with messages from the requested validators.

Custom Messages

Getting messages as an array is fine, but sometimes you need to customize them in order to present them to the user. This is possible using the findMessages() method as well:

For all messages, the {{name}} and {{input}} variable is available for templates.

Validator Name

On v::attribute() and v::key(), {{name}} is the attribute/key name. For others, is the same as the input. You can customize a validator name using:

Zend/Symfony Validators

It is also possible to reuse validators from other frameworks if they are installed:

Validation Methods

We've seen validate() that returns true or false and assert() that throws a complete validation report. There is also a check() method that returns an Exception only with the first error found:

Message:

"really messed up screen#name" must contain only letters (a-z) and digits (0-9)

Reference

Types

Generics

Comparing Values

Numeric

String

Arrays

Objects

Date and Time

Group Validators

Regional

Files

Other

Alphabetically

v::allOf($v1, $v2, $v3...)

Will validate if all inner validators validates.

This is similar to the chain (which is an allOf already), but its syntax allows you to set custom names for every node:

See also:

v::alnum()

v::alnum(string $additionalChars)

Validates alphanumeric characters from a-Z and 0-9.

A parameter for extra characters can be used:

This validator allows whitespace, if you want to remove them add ->noWhitespace() to the chain:

By default empty values are allowed, if you want to invalidate them, add ->notEmpty() to the chain:

You can restrict case using the ->lowercase() and ->uppercase() validators:

Message template for this validator includes {{additionalChars}} as the string of extra chars passed as the parameter.

See also:

v::alpha()

v::alpha(string $additionalChars)

This is similar to v::alnum(), but it doesn't allow numbers. It also accepts empty values and whitespace, so use v::notEmpty() and v::noWhitespace() when appropriate.

See also:

v::arr()

Validates if the input is an array or traversable object.

See also:

v::alwaysValid

Always returns true.

v::alwaysInvalid

Always return false.

v::attribute($name)

v::attribute($name, v $validator)

v::attribute($name, v $validator, boolean $mandatory=true)

Validates an object attribute.

You can also validate the attribute itself:

Third parameter makes the attribute presence optional:

The name of this validator is automatically set to the attribute name.

See also:

v::between($start, $end)

v::between($start, $end, boolean $inclusive=false)

Validates ranges. Most simple example:

The type as the first validator in a chain is a good practice, since between accepts many types:

Also very powerful with dates:

Date ranges accept strtotime values:

A third parameter may be passed to validate the passed values inclusive:

Message template for this validator includes {{minValue}} and {{maxValue}}.

See also:

v::bool()

Validates if the input is a boolean value:

v::call(callable $callback)

This is a very low level validator. It calls a function, method or closure for the input and then validates it. Consider the following variable:

To validate every part of this URL we could use the native parse_url function to break its parts:

This function returns an array containing scheme, host, path and query. We can validate them this way:

Using v::call() you can do this in a single chain:

It is possible to call methods and closures as the first parameter:

See also:

v::callback(callable $callback)

This is a wildcard validator, it uses a function name, method or closure to validate something:

(Please note that this is a sample, the v::int() validator is much better).

As in v::call(), you can pass a method or closure to it.

See also:

v::charset()

Validates if a string is in a specific charset.

The array format is a logic OR, not AND.

v::cnpj()

Validates the Brazillian CNPJ number. Ignores non-digit chars, so use ->digit() if needed.

See also:

v::nfeAccessKey()

Validates the access key of the Brazilian electronic invoice (NFe).

v::consonants() (deprecated)

Validates strings that contain only consonants. It's now deprecated, consonant should be used instead.

See also:

v::consonant()

v::consonant(string $additionalChars)

Similar to v::alnum(). Validates strings that contain only consonants:

See also:

v::contains($value)

v::contains($value, boolean $identical=false)

For strings:

For arrays:

A second parameter may be passed for identical comparison instead of equal comparison.

Message template for this validator includes {{containsValue}}.

See also:

v::cntrl

v::cntrl(string $additionalChars)

This is similar to v::alnum(), but only accepts control characters:

See also:

v::countryCode

Validates an ISO country code like US or BR.

See also:

v::cnh()

Validates a Brazillian driver's license.

See also:

v::cpf()

Validates a Brazillian CPF number.

It ignores any non-digit char:

If you need to validate digits only, add ->digit() to the chain:

See also:

v::creditCard()

Validates a credit card number.

It ignores any non-digit chars, so use ->digit() when appropriate.

v::date()

v::date($format)

Validates if input is a date:

Also accepts strtotime values:

And DateTime instances:

You can pass a format when validating strings:

Format has no effect when validating DateTime instances.

Message template for this validator includes {{format}}.

See also:

v::digits() (deprecated)

Validates 0-9, empty or whitespace only. It's now deprecated, digit should be used instead.

See also:

v::digit()

This is similar to v::alnum(), but it doesn't allow a-Z. It also accepts empty values and whitespace, so use v::notEmpty() and v::noWhitespace() when appropriate.

See also:

v::domain()

v::domain($checkTLD=true)

Validates domain names.

You can skip top level domain (TLD) checks to validate internal domain names:

This is a composite validator, it validates several rules internally:

Messages for this validator will reflect rules above.

See also:

v::directory()

Validates directories.

This validator will consider SplFileInfo instances, so you can do something like:

See also

v::each(v $validatorForValue)

v::each(null, v $validatorForKey)

v::each(v $validatorForValue, v $validatorForKey)

Iterates over an array or Iterator and validates the value or key of each entry:

Using arr() before each() is a best practice.

See also:

v::email()

Validates an email address.

v::exists()

Validates files or directories.

This validator will consider SplFileInfo instances, so you can do something like:

See also

v::endsWith($value)

v::endsWith($value, boolean $identical=false)

This validator is similar to v::contains(), but validates only if the value is at the end of the input.

For strings:

For arrays:

A second parameter may be passed for identical comparison instead of equal comparison.

Message template for this validator includes {{endValue}}.

See also:

v::equals($value)

v::equals($value, boolean $identical=false)

Validates if the input is equal some value.

Identical validation (===) is possible:

Message template for this validator includes {{compareTo}}.

See also:

v::even()

Validates an even number.

Using int() before even() is a best practice.

See also

v::file()

Validates files.

This validator will consider SplFileInfo instances, so you can do something like:

See also

v::float()

Validates a floating point number.

v::graph()

v::graph(string $additionalChars)

Validates all characters that are graphically represented.

See also:

v::hexa() (deprecated)

Validates an hexadecimal number. It's now deprecated, xdigit should be used instead.

See also:

v::in($haystack)

v::in($haystack, boolean $identical=false)

Validates if the input is contained in a specific haystack.

For strings:

For arrays:

A second parameter may be passed for identical comparison instead of equal comparison.

Message template for this validator includes {{haystack}}.

See also:

v::instance($instanceName)

Validates if the input is an instance of the given class or interface.

Message template for this validator includes {{instanceName}}.

See also:

v::int()

Validates if the input is an integer.

See also:

v::ip()

v::ip($options)

Validates IP Addresses. This validator uses the native filter_var() PHP function.

You can pass a parameter with filter_var flags for IP.

v::json()

Validates if the given input is a valid JSON.

v::key($name)

v::key($name, v $validator)

v::key($name, v $validator, boolean $mandatory=true)

Validates an array key.

You can also validate the key value itself:

Third parameter makes the key presence optional:

The name of this validator is automatically set to the key name.

See also:

v::leapDate($format)

Validates if a date is leap.

This validator accepts DateTime instances as well. The $format parameter is mandatory.

See also:

v::leapYear()

Validates if a year is leap.

This validator accepts DateTime instances as well.

See also:

v::length($min, $max)

v::length($min, null)

v::length(null, $max)

v::length($min, $max, boolean $inclusive=false)

Validates lengths. Most simple example:

You can also validate only minimum length:

Only maximum length:

The type as the first validator in a chain is a good practice, since length accepts many types:

A third parameter may be passed to validate the passed values inclusive:

Message template for this validator includes {{minValue}} and {{maxValue}}.

See also:

v::lowercase()

Validates if string characters are lowercase in the input:

See also:

v::macAddress()

Validates a Mac Address.

v::max()

v::max(boolean $inclusive=false)

Validates if the input doesn't exceed the maximum value.

Also accepts dates:

true may be passed as a parameter to indicate that inclusive values must be used.

Message template for this validator includes {{maxValue}}.

See also:

v::min()

v::min(boolean $inclusive=false)

Validates if the input doesn't exceed the minimum value.

Also accepts dates:

true may be passed as a parameter to indicate that inclusive values must be used.

Message template for this validator includes {{minValue}}.

See also:

v::minimumAge($age)

Validates a minimum age for a given date.

Using date() before is a best-practice.

Message template for this validator includes {{age}}.

See also:

v::multiple($multipleOf)

Validates if the input is a multiple of the given parameter

See also:

v::negative()

Validates if a number is lower than zero

See also:

v::noWhitespace()

Validates if a string contains no whitespace (spaces, tabs and line breaks);

Like other rules the input is still optional.

This is most useful when chaining with other validators such as v::alnum()

v::noneOf($v1, $v2, $v3...)

Validates if NONE of the given validators validate:

In the sample above, 'foo' isn't a integer nor a float, so noneOf returns true.

See also:

v::not(v $negatedValidator)

Negates any rule.

using a shortcut

In the sample above, validator returns true because 'foo' isn't an IP Address.

You can negate complex, grouped or chained validators as well:

using a shortcut

Each other validation has custom messages for negated rules.

See also:

v::notEmpty()

Validates if the given input is not empty or in other words is input mandatory and required. This function also takes whitespace into account, use noWhitespace() if no spaces or linebreaks and other whitespace anywhere in the input is desired.

Null values are empty:

Numbers:

Empty arrays:

Whitespace:

See also:

v::nullValue()

Validates if the input is null. This rule does not allow empty strings to avoid ambiguity.

See also:

v::numeric()

Validates on any numeric value.

See also:

v::object()

Validates if the input is an object.

See also:

v::odd()

Validates an odd number.

Using int() before odd() is a best practice.

See also

v::oneOf($v1, $v2, $v3...)

This is a group validator that acts as an OR operator.

In the sample above, v::int() doesn't validates, but v::float() validates, so oneOf returns true.

v::oneOf returns true if at least one inner validator passes.

Using a shortcut

See also:

v::perfectSquare()

Validates a perfect square.

v::phone()

Validates a valid 7, 10, 11 digit phone number (North America, Europe and most Asian and Middle East countries), supporting country and area codes (in dot, space or dashed notations) such as:

(555)555-5555
555 555 5555
+5(555)555.5555
33(1)22 22 22 22
+33(1)22 22 22 22
+33(020)7777 7777
03-6106666

v::positive()

Validates if a number is higher than zero

See also:

v::primeNumber()

Validates a prime number

v::prnt()

v::prnt(string $additionalChars)

Similar to v::graph but accepts whitespace.

See also:

v::punct()

v::punct(string $additionalChars)

Accepts only punctuation characters:

See also:

v::readable()

Validates if the given data is a file exists and is readable.

v::regex($regex)

Evaluates a regex on the input and validates if matches

Message template for this validator includes {{regex}}

v::roman()

Validates roman numbers

This validator ignores empty values, use notEmpty() when appropriate.

v::sf($sfValidator)

Use Symfony2 validators inside Respect\Validation flow. Messages are preserved.

You must add Symfony2 to your autoloading routines.

See also:

v::slug()

Validates slug-like strings:

v::space()

v::space(string $additionalChars)

Accepts only whitespace:

See also:

v::startsWith($value)

v::startsWith($value, boolean $identical=false)

This validator is similar to v::contains(), but validates only if the value is at the end of the.

For strings:

For arrays:

true may be passed as a parameter to indicate identical comparison instead of equal.

Message template for this validator includes {{startValue}}.

See also:

v::string()

Validates a string.

See also:

v::symbolicLink()

Validates if the given data is a path of a valid symbolic link.

v::tld()

Validates a top-level domain

See also

v::uploaded()

Validates if the given data is a file that was uploaded via HTTP POST.

v::uppercase()

Validates if string characters are uppercase in the input:

See also:

v::version()

Validates version numbers using Semantic Versioning.

v::vowels() (deprecated)

Validates strings that contains only vowels. It's now deprecated, vowel should be used instead.

See also:

v::vowel()

Similar to v::alnum(). Validates strings that contains only vowels:

See also:

v::when(v $if, v $then, v $else)

A ternary validator that accepts three parameters.

When the $if validates, returns validation for $then. When the $if doesn't validate, returns validation for $else.

In the sample above, if $input is an integer, then it must be positive. If $input is not an integer, then it must not me empty.

See also:

v::xdigit()

Accepts an hexadecimal number:

Notice, however, that it doesn't accept strings starting with 0x:

See also:

v::writable()

Validates if the given data is a file exists and is writable.

v::zend($zendValidator)

Use Zend validators inside Respect\Validation flow. Messages are preserved.

You need to put Zend Framework in your autoload routines.

See also:


All versions of validation with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package s-bhojani/validation contains the following files

Loading the files please wait ....