- Getting Started
- Blank Values
- Custom Rules
- Validator Reuse
- Available Sanitize Rules
- Available Validate Rules
The following classes can get used to validate a field on an object:
Note: examples below assume your files include
use Mbright\Validaiton\Rules\Validate
Validates the value as only containing alphabetic characters
$validator->validate('field')->is(new Validate\Alpha());
Validates that the value only contains alpha-numeric characters, underscores, and dashes
$validator->validate('field')->is(new Validate\AlphaDash());
Validates the value as containing alpha-numeric values only
$validator->validate('field')->is(new Validate\AlphaNum());
Validates that the value is within or equal to a minimum and maximum value
$validator->validate('field')->is(new Validate\Between(1, 100));
Validates the value as being a boolean, or a pseudo-boolean Pseudo-true values include the strings '1', 'y', 'yes', and 'true'; pseudo-false values include the strings '0', 'n', 'no', and 'false'
$validator->validate('field')->is(new Validate\Boolean());
Validates the value using a callable/callback The callable should take two arguments, $subject and $field, to indicate the subject and the field within that subject It should return true to pass, or false to fail
$callback = function ($subject, $field) {
if ($subject->field === 'foo') {
return true
}
return false;
};
$validator->validate('field')->is(new Validate\Callback($callback));
Note: this library will convert arrays to objects, so always use object notation ($subject->$field) and not array notation ($subject[$field]);
Validates the value as being a credit card number
$validator->validate('field')->is(new Validate\CreditCard());
Validates the value as being a valid representation of a date and/or time
$validator->validate('field')->is(new Validate\DateTime());
Validates the value as being a valid email according to the native filter_var
email filter
$validator->validate('field')->is(new Validate\Email());
Validates that the value is loosely equal (==) to the value of another field on the object
$validator->validate('field')->is(new Validate\EqualToField('otherField'));
Validates that the value is loosely equal (==) to the given value
$validator->validate('field')->is(new Validate\EqualToValue('foo'));
Validates that the value represents a float
$validator->validate('field')->is(Validate\FloatVal::class);
Validates that the value is a valid hex that is not longer than the maximum length
$validator->validate('hex')->is(new Validate\Hex());
Validates that the value loosely equals (==) to a key in the given array
$validator->validate('field')->is(new Validate\InKeys($array));
Validates that the value represents an integer
$validator->validate('field')->is(new Validate\Integer());
Validates that the value is strictly equal (===) to a value in the given array
$validator->validate('field')->is(new Validate\InValues($array));
Validates the value as an IPv4 or IPv6 address, allowing reserved and private addresses
$validator->validate('field')->is(new Validate\IpAddress());
To modify restrictions on the filter, pass the appropriate FILTER_FLAG_*
constants (seen here) as a second parameter
// only allow IPv4 addresses in the non-private range
$validator->validate('field')->is(new Validate\IpAddress(FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE));
// only allow IPv6 addresses in non-reserved range
$validator->validate('field')->is(new Validate\IpAddress(FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
Validates the value is a correct ISBN (International Standard Book Number)
$validator->validate('field')->is(new Validate\Isbn());
Validates the given value against a list of locale strings (internal to the rule class)
$validator->validate('field')->is(new Validate\Locale());
Validates the value as all lowercase
$validator->validate('field')->is(new Validate\Lowercase());
Validates the value begins with a lowercase character
$validator->validate('field')->is(new Validate\LowercaseFirst());
Validates the value as being less than or equal to a maximum
$validator->validate('field')->is(new Validate\Max(class, $max));
Validates the value as being greater than or equal to a minimum
$validator->validate('field')->is(new Validate\Min(class, $min));
Validates the value using preg_match()
$validator->validate('field')->is(new Validate\Regex($expr));
Validates the value as strictly equal (===
) to the value of another field in the subject
$validator->validate('field')->is(new Validate\StrictEqualToField('otherFieldName'));
Validates the value as strictly equal (===
) to a specified value
$validator->validate('field')->is(new Validate\StrictEqualToValue($value));
Validates the value can be represented by a string
$validator->validate('field')->is(new Validate\String());
Validates the value has a specified length
$validator->validate('field')->is(new Validate\Strlen($len));
Validates the value as being within or equal to a minimum and maximum length
$validator->validate('field')->is(new Validate\StrlenBetween($min, $max);
Validates the value length as being no longer than a maximum
$validator->validate('field')->is(new Validate\StrlenMax($max));
Validates the value length as being no shorter than a minimum
$validator->validate('field')->is(new Validate\StrlenMin($min));
Validates the value as title case
$validator->validate('field')->is(new Validate\TitleCase());
Validates the value is trim()
med Optionally specify characters to trim
$validator->validate('field')->is(new Validate\Trim($chars));
Validates the value represents a PHP upload information array, and that the file is an uploaded file
$validator->validate('field')->is(new Validate\Upload());
Validates the value as all uppercase
$validator->validate('field')->is(new Validate\UpperCase());
Validates the value begins with an uppercase character
$validator->validate('field')->is(new Validate\UpperCaseFirst());
Validates the value is a well-formed URL
$validator->validate('field')->is(new Validate\Url());
Validates that the value is a canonical human-readable UUID
$validator->validate('field')->is(new Validate\Uuid());
Validates that the value is a hex-only UUID
$validator->validate('field')->is(new UuidHexOnly());
Validates the value as being composed only of word characters
$validator->validate('field')->is(new Validate\Word());