PHP Class Fuel\Validation\Validator

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

Protected Properties

Property 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

Public Methods

Method 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

Protected Methods

Method 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 method

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 method

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 method

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 method

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 method

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
return string

createRuleInstance() public method

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

getField() public method

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

getFieldRules() public method

public getFieldRules ( string $fieldName ) : Fuel\Validation\RuleInterface[]
$fieldName string
return Fuel\Validation\RuleInterface[]

getGlobalMessage() public method

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

getRuleClassName() protected method

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

processMessageTokens() protected method

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
return string

removeGlobalMessage() public method

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

run() public method

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
return Fuel\Validation\ResultInterface

runField() public method

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
return Fuel\Validation\ResultInterface

setGlobalMessage() public method

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 method

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 method

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

validateField() protected method

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
return boolean

Property Details

$customRules protected_oe property

Contains a list of any custom validation rules
protected string[] $customRules
return string[]

$fields protected_oe property

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

$lastAddedField protected_oe property

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

$lastAddedRule protected_oe property

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

$messages protected_oe property

protected string[] $messages
return string[]

$ruleNamespace protected_oe property

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