Validation

The Validation component allows you to validate an arbitrary set of data. This component can be used to implement validation rules on data objects that do not belong to a model or collection.

Using the Validation Component

The following example shows its basic usage:

Initialising the Validator

For reusable code you can also write your own validator that extends Tomahawk's Validation class.

The validator can also take a Symfony\Component\Translation\TranslatorInterface as an optional parameter for translating the error messages.

Available Validators

Tomahawk comes with a few built-in validators. Each one takes 1 parameter which is an array of settings:

Name Explanation Full Class
Alpha Validates that field contains alphabetic characters Tomahawk\Validation\Constraints\Alpha
AlphaDash Validates that field contains alphanumeric characters, dashes and underscores Tomahawk\Validation\Constraints\AlphaDash
AlphaNumeric Validates that field contains alphanumeric characters Tomahawk\Validation\Constraints\AlphaNumeric
DateFormat Validates that field is in correct date format Tomahawk\Validation\Constraints\DateFormat
DigitsBetween Validates that field is between given digits Tomahawk\Validation\Constraints\DigitsBetween
Email Validates that field contains a valid email format Tomahawk\Validation\Constraints\Email
Image Validates that an uploaded file is an image Tomahawk\Validation\Constraints\Image
In Validates that a field is from a selection in an array Tomahawk\Validation\Constraints\In
NotIn Validates that a field is NOT from a selection in an array Tomahawk\Validation\Constraints\NotIn
IpAddress Validates that a field is a valid IPv4 Address Tomahawk\Validation\Constraints\IpAddress
IpAddress Validates that a field is a valid IPv4 Address Tomahawk\Validation\Constraints\IpAddress
Required Validates that a field’s value is not null, empty string or an empty array. Tomahawk\Validation\Constraints\Required
RequiredWith Validates that a field’s value is not null, empty string or an empty array when another field is present Tomahawk\Validation\Constraints\RequiredWith
RequiredWithout Validates that a field’s value is not null, empty string or an empty array when another field is NOT present Tomahawk\Validation\Constraints\RequiredWithout
Identical Validates that 2 field values are identical Tomahawk\Validation\Constraints\Identical
StringLength Validates the length of a string Tomahawk\Validation\Constraints\StringLength
Numerical Validates if a value is numeric Tomahawk\Validation\Constraints\Numerical
Integer Validates if a value is a number Tomahawk\Validation\Constraints\Integer
MinLength Validates the minimum length of a value Tomahawk\Validation\Constraints\MinLength
MaxLength Validates the maximum length of a value Tomahawk\Validation\Constraints\MaxLength
Regex Validates that the value of a field matches a regular expression Tomahawk\Validation\Constraints\Regex
TimeZone Validates that the value is a valid timeszone Tomahawk\Validation\Constraints\TimeZone
URL Validates that the value is a valid URL Tomahawk\Validation\Constraints\URL

Extending the Validator Rules

You may want to right your own validation rules. Its as simple as implement the Tomahawk\Validation\Constraints\ConstraintInterface interface.

There is also a Abstract Tomahawk\Validation\Constraints class with has some handy methods in it.