class Validator

Input validation.

Traits

Function parser trait.

Properties

protected array $input Input.
protected array $ruleSets Rule sets.
protected I18n $i18n I18n.
protected Container $container Container.
protected array $rules Rules.
protected array $originalFieldNames Original field names.
protected bool $isValid Is the input valid?
protected array $errors Error messages.

Methods

array
splitFunctionAndParameters(string $function)

Splits function name and parameters into an array.

array
parseFunction(string $function, bool|null $namedParameters = null)

Parses custom "function calls".

__construct(array $input, array $ruleSets, I18n|null $i18n = null, Container|null $container = null)

Constructor.

static string
rule(string $ruleName, mixed ...$parameters)

Rule builder.

extend(string $rule, string $ruleClass)

Registers a custom validation rule.

bool
hasWilcard(string $string)

Returns true if the field name has a wildcard and false if not.

saveOriginalFieldNames(array $fields, string $field)

Saves original field name along with the expanded field name.

string
getOriginalFieldName(string $field)

Returns the original field name.

array
expandFields(array $ruleSets)

Expands fields.

addRules(string $field, array $ruleSet)

Adds validation rules to input field.

addRulesIf(string $field, array $ruleSet, bool|Closure $condition)

Adds validation rules to input field if the condition is met.

object
parseRule(string $rule)

Parses the rule.

string
getRuleClassName(string $name)

Returns the rule class name.

ruleFactory(string $name, array $parameters)

Creates a rule instance.

bool
isInputFieldEmpty(mixed $value)

Returns true if the input field is considered empty and false if not.

string
getErrorMessage(RuleInterface $rule, string $field, object $parsedRule)

Returns the error message.

bool
validateField(string $field, string $rule)

Validates the field using the specified rule.

array
process()

Processes all validation rules and returns an array containing the validation status and potential error messages.

bool
isValid(array $errors = null)

Returns true if all rules passed and false if validation failed.

bool
isInvalid(array $errors = null)

Returns false if all rules passed and true if validation failed.

array
validate()

Validates the input and returns an array containing validated data.

array
getErrors()

Returns the validation errors.

Details

in FunctionParserTrait at line 32
protected array splitFunctionAndParameters(string $function)

Splits function name and parameters into an array.

Parameters

string $function Function

Return Value

array

Exceptions

RuntimeException

in FunctionParserTrait at line 52
protected array parseFunction(string $function, bool|null $namedParameters = null)

Parses custom "function calls".

The return value is an array consisting of the function name and parameters.

Parameters

string $function Function
bool|null $namedParameters Are we expecting named parameters?

Return Value

array

Exceptions

RuntimeException

at line 206
__construct(array $input, array $ruleSets, I18n|null $i18n = null, Container|null $container = null)

Constructor.

Parameters

array $input Input
array $ruleSets Rule sets
I18n|null $i18n I18n
Container|null $container Container

at line 224
static string rule(string $ruleName, mixed ...$parameters)

Rule builder.

Parameters

string $ruleName Rule name
mixed ...$parameters Rule parameters

Return Value

string

at line 241
Validator extend(string $rule, string $ruleClass)

Registers a custom validation rule.

Parameters

string $rule Rule
string $ruleClass Rule class

Return Value

Validator

at line 254
protected bool hasWilcard(string $string)

Returns true if the field name has a wildcard and false if not.

Parameters

string $string Field name

Return Value

bool

at line 265
protected saveOriginalFieldNames(array $fields, string $field)

Saves original field name along with the expanded field name.

Parameters

array $fields Expanded field names
string $field Original field name

at line 279
protected string getOriginalFieldName(string $field)

Returns the original field name.

Parameters

string $field Field name

Return Value

string

at line 290
protected array expandFields(array $ruleSets)

Expands fields.

Parameters

array $ruleSets Rule sets

Return Value

array

at line 323
Validator addRules(string $field, array $ruleSet)

Adds validation rules to input field.

Parameters

string $field Input field
array $ruleSet Rule set

Return Value

Validator

at line 338
Validator addRulesIf(string $field, array $ruleSet, bool|Closure $condition)

Adds validation rules to input field if the condition is met.

Parameters

string $field Input field
array $ruleSet Rule set
bool|Closure $condition Condition

Return Value

Validator

at line 354
protected object parseRule(string $rule)

Parses the rule.

Parameters

string $rule Rule

Return Value

object

at line 375
protected string getRuleClassName(string $name)

Returns the rule class name.

Parameters

string $name Rule name

Return Value

string

Exceptions

RuntimeException

at line 392
protected RuleInterface ruleFactory(string $name, array $parameters)

Creates a rule instance.

Parameters

string $name Rule name
array $parameters Rule parameters

Return Value

RuleInterface

at line 403
protected bool isInputFieldEmpty(mixed $value)

Returns true if the input field is considered empty and false if not.

Parameters

mixed $value Value

Return Value

bool

at line 416
protected string getErrorMessage(RuleInterface $rule, string $field, object $parsedRule)

Returns the error message.

Parameters

RuleInterface $rule Rule
string $field Field name
object $parsedRule Parsed rule

Return Value

string

at line 435
protected bool validateField(string $field, string $rule)

Validates the field using the specified rule.

Parameters

string $field Field name
string $rule Rule

Return Value

bool

at line 466
protected array process()

Processes all validation rules and returns an array containing the validation status and potential error messages.

Return Value

array

at line 494
bool isValid(array $errors = null)

Returns true if all rules passed and false if validation failed.

Parameters

array $errors &$errors If $errors is provided, then it is filled with all the error messages

Return Value

bool

at line 507
bool isInvalid(array $errors = null)

Returns false if all rules passed and true if validation failed.

Parameters

array $errors &$errors If $errors is provided, then it is filled with all the error messages

Return Value

bool

at line 520
array validate()

Validates the input and returns an array containing validated data.

Return Value

array

Exceptions

ValidationException

at line 545
array getErrors()

Returns the validation errors.

Return Value

array