FormValidator::validateFields
Usage
bool FormValidator::validateFields([ string $error_type = 'view' ], [ array $required = [] ], [ array $data_types = [] ], [ array $min_length = [] ], [ array $max_length = [] ], [ array $labels = [] ])Description
Will validate any HTML form based on fields required, data types, and min / max lengths.Parameters
Parameter | Required | Type | Description |
---|---|---|---|
$error_type | No | string | Can be either 'view' or 'error', and dfines whether any validation errors should be displayed as callouts on the next page, or if processing should stop and an error page displayed. |
$required | No | array | One dimensional array of form field names that are required and can not be left blank. |
$data_types | No | array | Associative array of which data types form fields are required to be. Keys are the form field names, and values are the data types. Supported data types are, 'alphanum', 'integer', 'decimal', 'email', 'url' |
$min_length | No | array | Associative array of minimum length of form values. Keys are the form field names, and values are the minimum length. |
$max_length | No | array | Associative array of maximum length of form values. Keys are the form field names, and values are the maximum length. |
$labels | No | array | Associative error of labels to use in exchange of form field names within any validation error messages. Keys are the form field name, and values are the labels to use within error messages. |
Return Value
Boolean that specifies whether or not form validation was successful.Examples
Form Field Validation
public function add(): ?Product
{
// Validate form fields
if (!$this->validator->validateFields(
'view',
['name', 'amount', 'category_id', 'quantity'],
['amount' => 'decimal', 'quantity' => 'integer'],
['name' => 3]
]) {
return null;
}
}