PHP Class Fuel\Validation\Validator

Since: 2.0
Author: Fuel Development Team
Inheritance: implements Fuel\Validation\ValidatableInterface
Afficher le fichier Open project: fuelphp/validation Class Usage Examples

Protected Properties

Свойство Type Description
$customRules string[] Contains a list of any custom validation rules
$fields Fuel\Validation\FieldInterface[] Contains a list of fields to be validated
$lastAddedField Fuel\Validation\FieldInterface Keeps track of the last field added for magic method chaining
$lastAddedRule Fuel\Validation\RuleInterface Keeps track of the last rule added for message setting
$messages string[]
$ruleNamespace string Default namespace to look for rules in when a rule is not known

Méthodes publiques

Méthode Description
__call ( string $name, array $arguments ) Allows validation rules to be dynamically added using method chaining.
addCustomRule ( string $name, string $class ) Adds custom validation rules and allows for core rules to be overridden.
addField ( string | Fuel\Validation\FieldInterface $field, string $label = null ) Adds a new field to the validation object
addRule ( string | Fuel\Validation\FieldInterface $field, Fuel\Validation\RuleInterface $rule ) Adds a rule that can be used to validate a field
createRuleInstance ( string $name, mixed $parameters = [] ) : Fuel\Validation\RuleInterface Creates an instance of the given rule name
getField ( $name ) : Fuel\Validation\FieldInterface Returns the given field
getFieldRules ( string $fieldName ) : Fuel\Validation\RuleInterface[]
getGlobalMessage ( string $ruleName ) : null | string Gets the global message set for a rule
removeGlobalMessage ( string $ruleName ) Removes a global rule message
run ( array $data, Fuel\Validation\ResultInterface $result = null ) : Fuel\Validation\ResultInterface Takes an array of data and validates that against the assigned rules.
runField ( string $field, array $data, Fuel\Validation\ResultInterface $result = null ) : Fuel\Validation\ResultInterface Takes a field name and an array of data and validates the field against the assigned rules.
setGlobalMessage ( string $ruleName, string | null $message ) Sets a custom message for all fields of the given type that are created after the message has been set.
setGlobalMessages ( string[] $messages ) Sets custom messages for one or more rules. Setting the value to "null" will remove the message
setMessage ( string $message ) Sets the failure message for the last added rule

Méthodes protégées

Méthode Description
buildMessage ( Fuel\Validation\FieldInterface $field, Fuel\Validation\RuleInterface $rule, $value ) : string Gets a Rule's message and processes that with various tokens
getRuleClassName ( string $name ) : string Returns the full class name for the given validation rule
processMessageTokens ( array $tokens, string $message ) : string Replaces any {} tokens with the matching value from $tokens.
validateField ( string $field, mixed[] $data, Fuel\Validation\ResultInterface $result ) : boolean Validates a single field

Method Details

__call() public méthode

Allows validation rules to be dynamically added using method chaining.
Since: 2.0
public __call ( string $name, array $arguments )
$name string
$arguments array

addCustomRule() public méthode

When wanting to override a core rule just specify the rule name as $name. Eg, 'required', 'minLength'. Note the lowercase first letter. The name of the rule should not contain any whitespace or special characters as the name will be available to use as a function name in the method chaining syntax.
Since: 2.0
public addCustomRule ( string $name, string $class )
$name string
$class string

addField() public méthode

Adds a new field to the validation object
Since: 2.0
public addField ( string | Fuel\Validation\FieldInterface $field, string $label = null )
$field string | Fuel\Validation\FieldInterface
$label string Field name to use in messages, set to null to use $field

addRule() public méthode

Adds a rule that can be used to validate a field
Since: 2.0
public addRule ( string | Fuel\Validation\FieldInterface $field, Fuel\Validation\RuleInterface $rule )
$field string | Fuel\Validation\FieldInterface
$rule Fuel\Validation\RuleInterface

buildMessage() protected méthode

Gets a Rule's message and processes that with various tokens
protected buildMessage ( Fuel\Validation\FieldInterface $field, Fuel\Validation\RuleInterface $rule, $value ) : string
$field Fuel\Validation\FieldInterface
$rule Fuel\Validation\RuleInterface
Résultat string

createRuleInstance() public méthode

Creates an instance of the given rule name
Since: 2.0
public createRuleInstance ( string $name, mixed $parameters = [] ) : Fuel\Validation\RuleInterface
$name string
$parameters mixed
Résultat Fuel\Validation\RuleInterface

getField() public méthode

Returns the given field
Since: 2.0
public getField ( $name ) : Fuel\Validation\FieldInterface
$name
Résultat Fuel\Validation\FieldInterface

getFieldRules() public méthode

public getFieldRules ( string $fieldName ) : Fuel\Validation\RuleInterface[]
$fieldName string
Résultat Fuel\Validation\RuleInterface[]

getGlobalMessage() public méthode

Gets the global message set for a rule
public getGlobalMessage ( string $ruleName ) : null | string
$ruleName string
Résultat null | string Will be null if there is no message

getRuleClassName() protected méthode

Returns the full class name for the given validation rule
Since: 2.0
protected getRuleClassName ( string $name ) : string
$name string
Résultat string

processMessageTokens() protected méthode

Replaces any {} tokens with the matching value from $tokens.
Since: 2.0
protected processMessageTokens ( array $tokens, string $message ) : string
$tokens array Associative array of token names and values
$message string
Résultat string

removeGlobalMessage() public méthode

Removes a global rule message
Since: 2.0
public removeGlobalMessage ( string $ruleName )
$ruleName string

run() public méthode

The array is expected to have keys named after fields. This function will call reset() before it runs.
Since: 2.0
public run ( array $data, Fuel\Validation\ResultInterface $result = null ) : Fuel\Validation\ResultInterface
$data array
$result Fuel\Validation\ResultInterface
Résultat Fuel\Validation\ResultInterface

runField() public méthode

The array is expected to have keys named after fields. This function will call reset() before it runs.
Since: 2.0
public runField ( string $field, array $data, Fuel\Validation\ResultInterface $result = null ) : Fuel\Validation\ResultInterface
$field string
$data array
$result Fuel\Validation\ResultInterface
Résultat Fuel\Validation\ResultInterface

setGlobalMessage() public méthode

Sets a custom message for all fields of the given type that are created after the message has been set.
Since: 2.0
public setGlobalMessage ( string $ruleName, string | null $message )
$ruleName string Name of the rule to set a message for, eg, required, number, exactLength
$message string | null Set to null to disable the custom message

setGlobalMessages() public méthode

Sets custom messages for one or more rules. Setting the value to "null" will remove the message
Since: 2.0
public setGlobalMessages ( string[] $messages )
$messages string[]

setMessage() public méthode

Sets the failure message for the last added rule
Since: 2.0
public setMessage ( string $message )
$message string

validateField() protected méthode

Validates a single field
Since: 2.0
protected validateField ( string $field, mixed[] $data, Fuel\Validation\ResultInterface $result ) : boolean
$field string
$data mixed[]
$result Fuel\Validation\ResultInterface
Résultat boolean

Property Details

$customRules protected_oe property

Contains a list of any custom validation rules
protected string[] $customRules
Résultat string[]

$fields protected_oe property

Contains a list of fields to be validated
protected FieldInterface[],Fuel\Validation $fields
Résultat Fuel\Validation\FieldInterface[]

$lastAddedField protected_oe property

Keeps track of the last field added for magic method chaining
protected FieldInterface,Fuel\Validation $lastAddedField
Résultat Fuel\Validation\FieldInterface

$lastAddedRule protected_oe property

Keeps track of the last rule added for message setting
protected RuleInterface,Fuel\Validation $lastAddedRule
Résultat Fuel\Validation\RuleInterface

$messages protected_oe property

protected string[] $messages
Résultat string[]

$ruleNamespace protected_oe property

Default namespace to look for rules in when a rule is not known
protected string $ruleNamespace
Résultat string