Skip to content

Latest commit

 

History

History
356 lines (225 loc) · 7.72 KB

ValidateRules.md

File metadata and controls

356 lines (225 loc) · 7.72 KB

Documentation Index

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

Alpha

Validates the value as only containing alphabetic characters

$validator->validate('field')->is(new Validate\Alpha());

AlphaDash

Validates that the value only contains alpha-numeric characters, underscores, and dashes

$validator->validate('field')->is(new Validate\AlphaDash());

AlphaNum

Validates the value as containing alpha-numeric values only

$validator->validate('field')->is(new Validate\AlphaNum());

Between($min, $max)

Validates that the value is within or equal to a minimum and maximum value

$validator->validate('field')->is(new Validate\Between(1, 100));

Boolean

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());

Callback

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]);

CreditCard

Validates the value as being a credit card number

$validator->validate('field')->is(new Validate\CreditCard());

DateTime

Validates the value as being a valid representation of a date and/or time

$validator->validate('field')->is(new Validate\DateTime());

Email

Validates the value as being a valid email according to the native filter_var email filter

$validator->validate('field')->is(new Validate\Email());

EqualToField($otherField)

Validates that the value is loosely equal (==) to the value of another field on the object

$validator->validate('field')->is(new Validate\EqualToField('otherField'));

EqualToValue($value)

Validates that the value is loosely equal (==) to the given value

$validator->validate('field')->is(new Validate\EqualToValue('foo'));

FloatVal

Validates that the value represents a float

$validator->validate('field')->is(Validate\FloatVal::class);

Hex($max = null)

Validates that the value is a valid hex that is not longer than the maximum length

$validator->validate('hex')->is(new Validate\Hex());

InKeys($array)

Validates that the value loosely equals (==) to a key in the given array

$validator->validate('field')->is(new Validate\InKeys($array));

Integer

Validates that the value represents an integer

$validator->validate('field')->is(new Validate\Integer());

InValues($array)

Validates that the value is strictly equal (===) to a value in the given array

$validator->validate('field')->is(new Validate\InValues($array));

IpAddress

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));

Isbn

Validates the value is a correct ISBN (International Standard Book Number)

$validator->validate('field')->is(new Validate\Isbn());

Locale

Validates the given value against a list of locale strings (internal to the rule class)

$validator->validate('field')->is(new Validate\Locale());

Lowercase

Validates the value as all lowercase

$validator->validate('field')->is(new Validate\Lowercase());

LowercaseFirst

Validates the value begins with a lowercase character

$validator->validate('field')->is(new Validate\LowercaseFirst());

Max($max)

Validates the value as being less than or equal to a maximum

$validator->validate('field')->is(new Validate\Max(class, $max));

Min($min)

Validates the value as being greater than or equal to a minimum

$validator->validate('field')->is(new Validate\Min(class, $min));

Regex($expr)

Validates the value using preg_match()

$validator->validate('field')->is(new Validate\Regex($expr));

StrictEqualToField($otherField)

Validates the value as strictly equal (===) to the value of another field in the subject

$validator->validate('field')->is(new Validate\StrictEqualToField('otherFieldName'));

StrictEqualToValue($value)

Validates the value as strictly equal (===) to a specified value

$validator->validate('field')->is(new Validate\StrictEqualToValue($value));

String

Validates the value can be represented by a string

$validator->validate('field')->is(new Validate\String());

Strlen($len)

Validates the value has a specified length

$validator->validate('field')->is(new Validate\Strlen($len));

StrlenBetween($min, $max)

Validates the value as being within or equal to a minimum and maximum length

$validator->validate('field')->is(new Validate\StrlenBetween($min, $max);

StrlenMax($max)

Validates the value length as being no longer than a maximum

$validator->validate('field')->is(new Validate\StrlenMax($max));

StrlenMin($min)

Validates the value length as being no shorter than a minimum

$validator->validate('field')->is(new Validate\StrlenMin($min));

TitleCase

Validates the value as title case

$validator->validate('field')->is(new Validate\TitleCase());

Trim($chars = ' \t\n\r\0\x0B')

Validates the value is trim()med Optionally specify characters to trim

$validator->validate('field')->is(new Validate\Trim($chars));

Upload

Validates the value represents a PHP upload information array, and that the file is an uploaded file

$validator->validate('field')->is(new Validate\Upload());

UpperCase

Validates the value as all uppercase

$validator->validate('field')->is(new Validate\UpperCase());

UpperCaseFirst

Validates the value begins with an uppercase character

$validator->validate('field')->is(new Validate\UpperCaseFirst());

Url

Validates the value is a well-formed URL

$validator->validate('field')->is(new Validate\Url());

Uuid

Validates that the value is a canonical human-readable UUID

$validator->validate('field')->is(new Validate\Uuid());

UuidHexOnly

Validates that the value is a hex-only UUID

$validator->validate('field')->is(new UuidHexOnly());

Word

Validates the value as being composed only of word characters

$validator->validate('field')->is(new Validate\Word());