FormField
in package
Represents an input field of a form on the server side
Table of Contents
- $dataType : string
- $dbField : string
- $isRequired : bool
- $name : string
- $noSave : bool
- $rules : array<string|int, object>
- $value : mixed
- __construct() : mixed
- Creates a form field representation
- date() : FormField
- Adds a date rule
- exists() : FormField
- Adds an exists rule
- file() : FormField
- Rule for validating file uploads. This rule must also be set when automatic uploads in insertDatabase is needed.
- filter() : FormField
- Adds a filter rule
- integer() : FormField
- Adds an integer rule
- length() : FormField
- Adds a length rule
- range() : FormField
- Adds a range rule
- regex() : FormField
- Adds a regular expression rule to the field
- required() : FormField
- Adds a required rule
- unique() : FormField
- Adds a unique rule
Properties
$dataType
public
string
$dataType
Datatype needed for prepared statements (s/i...)
$dbField
public
string
$dbField
Name of the field in the database
$isRequired
public
bool
$isRequired
If set, the value is required. This is saved outside the rules array because other rules may need access to this value to work properly
$name
public
string
$name
Name of the input in the post request
$noSave
public
bool
$noSave
Skip this field when writing SQL
$rules
public
array<string|int, object>
$rules
Array of rules that specify how to validate this form field.
$value
public
mixed
$value
The validated value
Methods
__construct()
Creates a form field representation
public
__construct(string $name[, string $dbName = null ]) : mixed
Parameters
- $name : string
-
Name of the field. Should match the name in the post header
- $dbName : string = null
-
Name of the field in the database. If not set it will be equal to the name
Return values
mixed —date()
Adds a date rule
public
date([string $format = "Y-m-d" ]) : FormField
This rule checks if the input value adheres to a given date format
Parameters
- $format : string = "Y-m-d"
-
The tested date format
Return values
FormField —Returns itself to allow chaining
exists()
Adds an exists rule
public
exists(string $table, string $field) : FormField
This rule checks if a dataset with a specific value already exists. A set to ignore can also be specified. An error will be created when the set does not exist.
Parameters
- $table : string
-
Table name in the database
- $field : string
-
Field name in the table
Return values
FormField —Returns itself to allow chaining
file()
Rule for validating file uploads. This rule must also be set when automatic uploads in insertDatabase is needed.
public
file(int $maxSize, array<string|int, string> $types) : FormField
Parameters
- $maxSize : int
-
The maximum allowed file size. Constants for this are available.
- $types : array<string|int, string>
-
Accepted file types
Return values
FormField —Returns itself to allow chaining
filter()
Adds a filter rule
public
filter(int $filter) : FormField
Creates an error when a filter fails. All filter_var compatible filters are available.
Parameters
- $filter : int
-
A valid PHP filter
Return values
FormField —Returns itself to allow chaining
integer()
Adds an integer rule
public
integer() : FormField
This rule will create an error when the input was not a value that could be parsed to an integer. Also this function sets the type of the field to "i".
Return values
FormField —Returns itself to allow chaining
length()
Adds a length rule
public
length(int $min, int $max) : FormField
This rule will create an error when the input is too long or too short
Parameters
- $min : int
-
Minimum number of chars
- $max : int
-
Maximum number of chars
Return values
FormField —Returns itself to allow chaining
range()
Adds a range rule
public
range(float $min, float $max) : FormField
This rule checks if the input, as a number, is within a range. If the number is not in the range, an error will be created.
Parameters
- $min : float
-
Min allowed value
- $max : float
-
Max allowed value
Return values
FormField —Returns itself to allow chaining
regex()
Adds a regular expression rule to the field
public
regex(string $expression[, array<string|int, string> $exceptions = [] ]) : FormField
Parameters
- $expression : string
-
The regex expression
- $exceptions : array<string|int, string> = []
-
An array of characters to be excluded from the regex
Return values
FormField —Returns itself to allow chaining
required()
Adds a required rule
public
required() : FormField
With this rule an error is created when no input for this field is given.
Return values
FormField —Returns itself to allow chaining
unique()
Adds a unique rule
public
unique(string $table, string $field[, string $ignoreField = null ][, string $ignoreValue = null ]) : FormField
This rule checks if a dataset with a specific value already exists. A set to ignore can also be specified. An error will be created when the set exists.
Parameters
- $table : string
-
Table name in the database
- $field : string
-
Field name in the table
- $ignoreField : string = null
-
name of the field in which the ignore value is
- $ignoreValue : string = null
-
value of the dataset that should be ignored
Return values
FormField —Returns itself to allow chaining