Validator
in package
Used with the {@see Validity} class.
Tags
Table of Contents
Properties
- $data : mixed
- $field_errors : mixed
- $general_errors : mixed
- $labels : mixed
Methods
- __construct() : mixed
- addArrayFieldError() : mixed
- Add an error message for a given field to the field errors list This variation is for array validation, e.g. an array of integers
- addFieldError() : mixed
- Add an error message for a given field to the field errors list
- addGeneralError() : mixed
- Add a general error message, e.g. for errors affecting many fields
- addMultipleFieldError() : mixed
- Add an error message from a multiple-field validation (e.g. checking at least one is set)
- arrayCheck() : array<int|string, bool>
- check() : bool
- Check the value of a field against a validation method, storing any error messages received Additional arguments are passed to the underlying method
- createNotifications() : mixed
- Create notification error messages for each error
- errorsAsArray() : array<string|int, string>
- Return all errors (general and field) as a mapped array.
- fromValidationException() : void
- Populates form field errors from ValidationException
- getFieldErrors() : array<string|int, mixed>
- Get an array of all field errors, indexed by field name Fields may have multiple errors defined
- getGeneralErrors() : array<string|int, mixed>
- Get an array of all general errors
- hasErrors() : bool
- isEmpty() : bool
- Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't
- multipleCheck() : bool
- Check multiple fields against a validation method
- required() : mixed
- Checks various fields are required If a field is required and no value is provided, no other validation will be proessed for that field.
- setData() : mixed
- Update the data to validate
- setFieldLabel() : mixed
- Set the label for a single field
- setFieldValue() : mixed
- Set the value for a single data field
- setLabels() : mixed
- Field labels make error messages a little friendlier
- trim() : array<string|int, mixed>
- Recursive trim data
- expandNs() : callable
- For a given function, expand the namespace for Sprout helpers
Properties
$data
protected
mixed
$data
$field_errors
protected
mixed
$field_errors
$general_errors
protected
mixed
$general_errors
$labels
protected
mixed
$labels
Methods
__construct()
public
__construct(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
-
Data to validate
addArrayFieldError()
Add an error message for a given field to the field errors list This variation is for array validation, e.g. an array of integers
public
addArrayFieldError(string $field_name, int $index, string $message) : mixed
Parameters
- $field_name : string
-
The field to add the error message for
- $index : int
-
The array index of the field to report error for
- $message : string
-
The message text
addFieldError()
Add an error message for a given field to the field errors list
public
addFieldError(string $field_name, string $message) : mixed
Parameters
- $field_name : string
-
The field to add the error message for
- $message : string
-
The message text
addGeneralError()
Add a general error message, e.g. for errors affecting many fields
public
addGeneralError(string $message) : mixed
Parameters
- $message : string
-
The message text
addMultipleFieldError()
Add an error message from a multiple-field validation (e.g. checking at least one is set)
public
addMultipleFieldError(array<string|int, mixed> $fields, string $message) : mixed
Parameters
- $fields : array<string|int, mixed>
-
The fields to add the error message for
- $message : string
-
The message text
arrayCheck()
public
arrayCheck(string $field_name, callable $func, mixed ...$args) : array<int|string, bool>
Parameters
- $field_name : string
- $func : callable
- $args : mixed
Return values
array<int|string, bool>check()
Check the value of a field against a validation method, storing any error messages received Additional arguments are passed to the underlying method
public
check(string $field_name, callable $func, mixed ...$args) : bool
Methods which are on classes within the Sprout\Helpers namespace do not need the namespace specified on the function name
If a field has already been checked with Validator::required and the field was empty, this function will not report errors (but will still return an appropriate value)
If a empty value is provided, it is not validated - returns true
Parameters
- $field_name : string
-
The field to check
- $func : callable
-
The function or method to call.
- $args : mixed
Return values
bool —True if validation was successful, false if it failed
createNotifications()
Create notification error messages for each error
public
createNotifications([string $scope = 'default' ]) : mixed
Parameters
- $scope : string = 'default'
-
Set the scope for the notifications
errorsAsArray()
Return all errors (general and field) as a mapped array.
public
errorsAsArray() : array<string|int, string>
Return values
array<string|int, string> —[ 'label' => 'messages' ]
fromValidationException()
Populates form field errors from ValidationException
public
fromValidationException(ValidationException $exception) : void
Parameters
- $exception : ValidationException
getFieldErrors()
Get an array of all field errors, indexed by field name Fields may have multiple errors defined
public
getFieldErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>getGeneralErrors()
Get an array of all general errors
public
getGeneralErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>hasErrors()
public
hasErrors() : bool
Return values
bool —True if there were any validation errors, false if there wasn't
isEmpty()
Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't
public
static isEmpty(mixed $val) : bool
Parameters
- $val : mixed
Return values
bool —True if empty, false if not.
multipleCheck()
Check multiple fields against a validation method
public
multipleCheck(array<string|int, mixed> $fields, callable $func, mixed ...$args) : bool
This is similar to Validator::check but it's designed for different validation methods, which work on a set of fields instead of a single field (e.g. Validity::oneRequired)
Additional arguments are passed to the underlying method Methods which are on classes within the Sprout\Helpers namespace do not need the namespace specified on the function name
Parameters
- $fields : array<string|int, mixed>
-
The fields to check
- $func : callable
-
The function or method to call.
- $args : mixed
-
additional args passed after the data value array
Return values
bool —True if validation was successful, false if it failed
required()
Checks various fields are required If a field is required and no value is provided, no other validation will be proessed for that field.
public
required(array<string|int, mixed> $fields) : mixed
Parameters
- $fields : array<string|int, mixed>
-
Fields to check
setData()
Update the data to validate
public
setData(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
-
Data to validate
setFieldLabel()
Set the label for a single field
public
setFieldLabel(string $field, string|null $label) : mixed
Parameters
- $field : string
-
The field to set
- $label : string|null
-
The label to set on the field
setFieldValue()
Set the value for a single data field
public
setFieldValue(string $field, mixed $value) : mixed
Parameters
- $field : string
-
The field to set
- $value : mixed
-
The value to set on the field
setLabels()
Field labels make error messages a little friendlier
public
setLabels(array<string|int, mixed> $labels) : mixed
Parameters
- $labels : array<string|int, mixed>
-
Field labels
trim()
Recursive trim data
public
static trim(array<string|int, mixed> &$data) : array<string|int, mixed>
Alters in-place AND returns the array This allows for use such as:
$_SESSION['register']['field_values'] = Validator::trim($_POST);
When used like this, the session gets set and the POST data is also trimmed, so can be used directly for database inserts.
Parameters
- $data : array<string|int, mixed>
-
Data to trim. Passed by-reference.
Return values
array<string|int, mixed> —Trimmed data
expandNs()
For a given function, expand the namespace for Sprout helpers
protected
expandNs(callable|string $func) : callable
Parameters
- $func : callable|string
-
The function to expand with the Sprout\Helpers namespace. Strings representing function names are affected; closures are not.
Return values
callable —$func