PHP Class Valitron\Validator

Validates input against certain criteria
Author: Vance Lucas ([email protected])
Show file Open project: vlucas/valitron Class Usage Examples

Protected Properties

Property Type Description
$_errors array
$_fields array
$_instanceRuleMessage array Contains all rule messages that are available to the current valitron instance
$_instanceRules array Contains all rules that are available to the current valitron instance.
$_labels array
$_lang string
$_langDir string
$_ruleMessages array
$_rules array
$_validations array
$validUrlPrefixes array

Public Methods

Method Description
__construct ( array $data, array $fields = [], string $lang = null, string $langDir = null ) Setup validation
addInstanceRule ( string $name, mixed $callback, string $message = null ) Adds a new validation rule callback that is tied to the current instance only.
addRule ( string $name, mixed $callback, string $message = null ) Register new validation rule callback
data ( ) : array Get array of fields and data
error ( string $field, string $msg, array $params = [] ) Add an error to error messages array
errors ( null | string $field = null ) : array | boolean Get array of error messages
getUniqueRuleName ( $fields )
hasValidator ( string $name ) : boolean Returns true if either a valdiator with the given name has been registered or there is a default validator by that name.
label ( string $value )
labels ( array $labels = [] )
lang ( string $lang = null ) : string Get/set language to use for validation messages
langDir ( string $dir = null ) : string Get/set language file path
message ( string $msg ) Specify validation message to use for error for the last validation rule
reset ( ) Reset object properties
rule ( string | callback $rule, array $fields ) Convenience method to add a single validation rule
rules ( array $rules ) Convenience method to add multiple validation rules with an array
validate ( ) : boolean Run validations and return boolean result
withData ( array $data, array $fields = [] ) : Valitron Replace data on cloned instance

Protected Methods

Method Description
assertRuleCallback ( $callback )
checkAndSetLabel ( string $field, string $msg, array $params ) : array
getPart ( $data, $identifiers )
getRuleMessages ( ) : array Returns all rule message, the static and instance ones.
getRules ( ) : array Returns all rule callbacks, the static and instance ones.
hasRule ( string $name, string $field ) : boolean Determine whether a field is being validated by the given rule.
stringLength ( string $value ) : integer | false Get the length of a string
validateAccepted ( string $field, mixed $value ) : boolean Validate that a field was "accepted" (based on PHP's string evaluation rules)
validateAlpha ( string $field, mixed $value ) : boolean Validate that a field contains only alphabetic characters
validateAlphaNum ( string $field, mixed $value ) : boolean Validate that a field contains only alpha-numeric characters
validateArray ( string $field, mixed $value ) : boolean Validate that a field is an array
validateBetween ( string $field, mixed $value, array $params ) : boolean Validate the size of a field is between min and max values
validateBoolean ( string $field, mixed $value ) : boolean Validate that a field contains a boolean.
validateContains ( string $field, mixed $value, array $params ) : boolean Validate a field contains a given string
validateCreditCard ( string $field, mixed $value, array $params ) : boolean Validate that a field contains a valid credit card optionally filtered by an array
validateDate ( string $field, mixed $value ) : boolean Validate that a field is a valid date
validateDateAfter ( string $field, mixed $value, array $params ) : boolean Validate the date is after a given date
validateDateBefore ( string $field, mixed $value, array $params ) : boolean Validate the date is before a given date
validateDateFormat ( string $field, mixed $value, array $params ) : boolean Validate that a field matches a date format
validateDifferent ( string $field, mixed $value, array $params ) : boolean Validate that a field is different from another field
validateEmail ( string $field, mixed $value ) : boolean Validate that a field is a valid e-mail address
validateEquals ( string $field, mixed $value, array $params ) : boolean Validate that two values match
validateIn ( string $field, mixed $value, array $params ) : boolean Validate a field is contained within a list of values
validateInstanceOf ( $field, $value, $params )
validateInteger ( string $field, mixed $value ) : boolean Validate that a field is an integer
validateIp ( string $field, mixed $value ) : boolean Validate that a field is a valid IP address
validateLength ( string $field, mixed $value, array $params ) : boolean Validate the length of a string
validateLengthBetween ( string $field, mixed $value, array $params ) : boolean Validate the length of a string (between)
validateLengthMax ( string $field, mixed $value, array $params ) : boolean Validate the length of a string (max)
validateLengthMin ( string $field, mixed $value, array $params ) : boolean Validate the length of a string (min)
validateMax ( string $field, mixed $value, array $params ) : boolean Validate the size of a field is less than a maximum value
validateMin ( string $field, mixed $value, array $params ) : boolean Validate the size of a field is greater than a minimum value.
validateNotIn ( string $field, mixed $value, array $params ) : boolean Validate a field is not contained within a list of values
validateNumeric ( string $field, mixed $value ) : boolean Validate that a field is numeric
validateOptional ( $field, $value, $params ) Validate optional field
validateRegex ( string $field, mixed $value, array $params ) : boolean Validate that a field passes a regular expression check
validateRequired ( string $field, mixed $value ) : boolean Required field validator
validateSlug ( string $field, mixed $value ) : boolean Validate that a field contains only alpha-numeric characters, dashes, and underscores
validateUrl ( string $field, mixed $value ) : boolean Validate that a field is a valid URL by syntax
validateUrlActive ( string $field, mixed $value ) : boolean Validate that a field is an active URL by verifying DNS record

Method Details

__construct() public method

Setup validation
public __construct ( array $data, array $fields = [], string $lang = null, string $langDir = null )
$data array
$fields array
$lang string
$langDir string

addInstanceRule() public method

Adds a new validation rule callback that is tied to the current instance only.
public addInstanceRule ( string $name, mixed $callback, string $message = null )
$name string
$callback mixed
$message string

addRule() public static method

Register new validation rule callback
public static addRule ( string $name, mixed $callback, string $message = null )
$name string
$callback mixed
$message string

assertRuleCallback() protected static method

protected static assertRuleCallback ( $callback )

checkAndSetLabel() protected method

protected checkAndSetLabel ( string $field, string $msg, array $params ) : array
$field string
$msg string
$params array
return array

data() public method

Get array of fields and data
public data ( ) : array
return array

error() public method

Add an error to error messages array
public error ( string $field, string $msg, array $params = [] )
$field string
$msg string
$params array

errors() public method

Get array of error messages
public errors ( null | string $field = null ) : array | boolean
$field null | string
return array | boolean

getPart() protected method

protected getPart ( $data, $identifiers )

getRuleMessages() protected method

Returns all rule message, the static and instance ones.
protected getRuleMessages ( ) : array
return array

getRules() protected method

Returns all rule callbacks, the static and instance ones.
protected getRules ( ) : array
return array

getUniqueRuleName() public method

public getUniqueRuleName ( $fields )

hasRule() protected method

Determine whether a field is being validated by the given rule.
protected hasRule ( string $name, string $field ) : boolean
$name string The name of the rule
$field string The name of the field
return boolean

hasValidator() public method

Returns true if either a valdiator with the given name has been registered or there is a default validator by that name.
public hasValidator ( string $name ) : boolean
$name string
return boolean

label() public method

public label ( string $value )
$value string

labels() public method

public labels ( array $labels = [] )
$labels array

lang() public static method

Get/set language to use for validation messages
public static lang ( string $lang = null ) : string
$lang string
return string

langDir() public static method

Get/set language file path
public static langDir ( string $dir = null ) : string
$dir string
return string

message() public method

Specify validation message to use for error for the last validation rule
public message ( string $msg )
$msg string

reset() public method

Reset object properties
public reset ( )

rule() public method

Convenience method to add a single validation rule
public rule ( string | callback $rule, array $fields )
$rule string | callback
$fields array

rules() public method

Convenience method to add multiple validation rules with an array
public rules ( array $rules )
$rules array

stringLength() protected method

Get the length of a string
protected stringLength ( string $value ) : integer | false
$value string
return integer | false

validate() public method

Run validations and return boolean result
public validate ( ) : boolean
return boolean

validateAccepted() protected method

This validation rule implies the field is "required"
protected validateAccepted ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateAlpha() protected method

Validate that a field contains only alphabetic characters
protected validateAlpha ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateAlphaNum() protected method

Validate that a field contains only alpha-numeric characters
protected validateAlphaNum ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateArray() protected method

Validate that a field is an array
protected validateArray ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateBetween() protected method

Validate the size of a field is between min and max values
protected validateBetween ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateBoolean() protected method

Validate that a field contains a boolean.
protected validateBoolean ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateContains() protected method

Validate a field contains a given string
protected validateContains ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateCreditCard() protected method

Validate that a field contains a valid credit card optionally filtered by an array
protected validateCreditCard ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateDate() protected method

Validate that a field is a valid date
protected validateDate ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateDateAfter() protected method

Validate the date is after a given date
protected validateDateAfter ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateDateBefore() protected method

Validate the date is before a given date
protected validateDateBefore ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateDateFormat() protected method

Validate that a field matches a date format
protected validateDateFormat ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateDifferent() protected method

Validate that a field is different from another field
protected validateDifferent ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateEmail() protected method

Validate that a field is a valid e-mail address
protected validateEmail ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateEquals() protected method

Validate that two values match
protected validateEquals ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateIn() protected method

Validate a field is contained within a list of values
protected validateIn ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateInstanceOf() protected method

protected validateInstanceOf ( $field, $value, $params )

validateInteger() protected method

Validate that a field is an integer
protected validateInteger ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateIp() protected method

Validate that a field is a valid IP address
protected validateIp ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateLength() protected method

Validate the length of a string
protected validateLength ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateLengthBetween() protected method

Validate the length of a string (between)
protected validateLengthBetween ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateLengthMax() protected method

Validate the length of a string (max)
protected validateLengthMax ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateLengthMin() protected method

Validate the length of a string (min)
protected validateLengthMin ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateMax() protected method

Validate the size of a field is less than a maximum value
protected validateMax ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateMin() protected method

Validate the size of a field is greater than a minimum value.
protected validateMin ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateNotIn() protected method

Validate a field is not contained within a list of values
protected validateNotIn ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateNumeric() protected method

Validate that a field is numeric
protected validateNumeric ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateOptional() protected method

Validate optional field
protected validateOptional ( $field, $value, $params )

validateRegex() protected method

Validate that a field passes a regular expression check
protected validateRegex ( string $field, mixed $value, array $params ) : boolean
$field string
$value mixed
$params array
return boolean

validateRequired() protected method

Required field validator
protected validateRequired ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateSlug() protected method

Validate that a field contains only alpha-numeric characters, dashes, and underscores
protected validateSlug ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateUrl() protected method

Validate that a field is a valid URL by syntax
protected validateUrl ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

validateUrlActive() protected method

Validate that a field is an active URL by verifying DNS record
protected validateUrlActive ( string $field, mixed $value ) : boolean
$field string
$value mixed
return boolean

withData() public method

Replace data on cloned instance
public withData ( array $data, array $fields = [] ) : Valitron
$data array
$fields array
return Valitron

Property Details

$_errors protected property

protected array $_errors
return array

$_fields protected property

protected array $_fields
return array

$_instanceRuleMessage protected property

Contains all rule messages that are available to the current valitron instance
protected array $_instanceRuleMessage
return array

$_instanceRules protected property

Contains all rules that are available to the current valitron instance.
protected array $_instanceRules
return array

$_labels protected property

protected array $_labels
return array

$_lang protected static property

protected static string $_lang
return string

$_langDir protected static property

protected static string $_langDir
return string

$_ruleMessages protected static property

protected static array $_ruleMessages
return array

$_rules protected static property

protected static array $_rules
return array

$_validations protected property

protected array $_validations
return array

$validUrlPrefixes protected property

protected array $validUrlPrefixes
return array