PHP Class Valitron\Validator

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

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode 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 méthode

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

addInstanceRule() public méthode

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 méthode

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

assertRuleCallback() protected static méthode

protected static assertRuleCallback ( $callback )

checkAndSetLabel() protected méthode

protected checkAndSetLabel ( string $field, string $msg, array $params ) : array
$field string
$msg string
$params array
Résultat array

data() public méthode

Get array of fields and data
public data ( ) : array
Résultat array

error() public méthode

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

errors() public méthode

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

getPart() protected méthode

protected getPart ( $data, $identifiers )

getRuleMessages() protected méthode

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

getRules() protected méthode

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

getUniqueRuleName() public méthode

public getUniqueRuleName ( $fields )

hasRule() protected méthode

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
Résultat boolean

hasValidator() public méthode

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
Résultat boolean

label() public méthode

public label ( string $value )
$value string

labels() public méthode

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

lang() public static méthode

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

langDir() public static méthode

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

message() public méthode

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

reset() public méthode

Reset object properties
public reset ( )

rule() public méthode

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

rules() public méthode

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

stringLength() protected méthode

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

validate() public méthode

Run validations and return boolean result
public validate ( ) : boolean
Résultat boolean

validateAccepted() protected méthode

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

validateAlpha() protected méthode

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

validateAlphaNum() protected méthode

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

validateArray() protected méthode

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

validateBetween() protected méthode

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
Résultat boolean

validateBoolean() protected méthode

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

validateContains() protected méthode

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

validateCreditCard() protected méthode

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
Résultat boolean

validateDate() protected méthode

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

validateDateAfter() protected méthode

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

validateDateBefore() protected méthode

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

validateDateFormat() protected méthode

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

validateDifferent() protected méthode

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

validateEmail() protected méthode

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

validateEquals() protected méthode

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

validateIn() protected méthode

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
Résultat boolean

validateInstanceOf() protected méthode

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

validateInteger() protected méthode

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

validateIp() protected méthode

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

validateLength() protected méthode

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

validateLengthBetween() protected méthode

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

validateLengthMax() protected méthode

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

validateLengthMin() protected méthode

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

validateMax() protected méthode

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
Résultat boolean

validateMin() protected méthode

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
Résultat boolean

validateNotIn() protected méthode

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
Résultat boolean

validateNumeric() protected méthode

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

validateOptional() protected méthode

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

validateRegex() protected méthode

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

validateRequired() protected méthode

Required field validator
protected validateRequired ( string $field, mixed $value ) : boolean
$field string
$value mixed
Résultat boolean

validateSlug() protected méthode

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

validateUrl() protected méthode

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

validateUrlActive() protected méthode

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

withData() public méthode

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

Property Details

$_errors protected_oe property

protected array $_errors
Résultat array

$_fields protected_oe property

protected array $_fields
Résultat array

$_instanceRuleMessage protected_oe property

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

$_instanceRules protected_oe property

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

$_labels protected_oe property

protected array $_labels
Résultat array

$_lang protected_oe static_oe property

protected static string $_lang
Résultat string

$_langDir protected_oe static_oe property

protected static string $_langDir
Résultat string

$_ruleMessages protected_oe static_oe property

protected static array $_ruleMessages
Résultat array

$_rules protected_oe static_oe property

protected static array $_rules
Résultat array

$_validations protected_oe property

protected array $_validations
Résultat array

$validUrlPrefixes protected_oe property

protected array $validUrlPrefixes
Résultat array