Field
in package
Represents an input field of a form on the server side
Table of Contents
- $dataType : string
- $dbField : string
- $isFile : bool
- $isRequired : bool
- $name : string
- $noSave : bool
- $rules : array<string|int, object>
- $value : mixed
- __construct() : mixed
- Creates a form field representation
- checked() : Field
- Adds a "must be ticked" rule for checkboxes.
- date() : Field
- Adds a date rule
- exists() : Field
- Adds an exists rule
- file() : Field
- Rule for validating file uploads. This rule must also be set when automatic uploads in insertDatabase is needed.
- filter() : Field
- Adds a filter rule
- in() : Field
- Adds an `in` rule
- integer() : Field
- Adds an integer rule
- length() : Field
- Adds a length rule
- range() : Field
- Adds a range rule
- regex() : Field
- Adds a regular expression rule to the field
- required() : Field
- Adds a required rule
- unique() : Field
- 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
$isFile
public
bool
$isFile
= false
Mark a form field as a file
$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 —checked()
Adds a "must be ticked" rule for checkboxes.
public
checked() : Field
A checkbox field always submits "1" (ticked) or "0" (unticked),
so required() is a no-op for it. Use this to enforce that the box
is actually ticked (e.g. accept-terms). Sugar over in() with the
common truthy representations.
Return values
Field —Returns itself to allow chaining
date()
Adds a date rule
public
date([string $format = "Y-m-d" ]) : Field
This rule checks if the input value adheres to a given date format.
Does not support array values (e.g. multi-select) — strtotime()
expects a scalar.
Parameters
- $format : string = "Y-m-d"
-
The tested date format
Return values
Field —Returns itself to allow chaining
exists()
Adds an exists rule
public
exists(string $table, string $field) : Field
This rule checks if a dataset with a specific value already exists
in the database. For array-valued fields (e.g. multi-select) the
rule is applied per item, every picked entry must exist.
Parameters
- $table : string
-
Table name in the database
- $field : string
-
Field name in the table
Return values
Field —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 = [] ]) : Field
Parameters
- $maxSize : int
-
The maximum allowed file size. Constants for this are available.
- $types : array<string|int, string> = []
-
Accepted file types
Return values
Field —Returns itself to allow chaining
filter()
Adds a filter rule
public
filter(int $filter) : Field
Creates an error when a filter fails. All filter_var compatible filters are available.
Does not support array values (e.g. multi-select) — filter_var()
expects a scalar; pair multi-select with ->in() instead.
Parameters
- $filter : int
-
A valid PHP filter
Return values
Field —Returns itself to allow chaining
in()
Adds an `in` rule
public
in(array<string|int, mixed> $allowedValues) : Field
The submitted value must appear in the given in-memory allow-list.
Unlike exists() this does not hit the database. Useful to guard
select / multi-select fields against tampered POST payloads
where the client sent an option that wasn't in the rendered
dropdown. For array-valued fields the rule is applied per item:
every picked entry must be in the allow-list.
Parameters
- $allowedValues : array<string|int, mixed>
-
The list of allowed values
Return values
Field —Returns itself to allow chaining
integer()
Adds an integer rule
public
integer() : Field
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".
Does not support array values (e.g. multi-select) — filter_var()
and intval() expect a scalar.
Return values
Field —Returns itself to allow chaining
length()
Adds a length rule
public
length(int $min, int $max) : Field
This rule will create an error when the input is too long or too short.
For array-valued fields (e.g. multi-select) length is measured as
the item count (count($value)) instead of the character count, so
->length(1, 3) means "between 1 and 3 selections."
Parameters
- $min : int
-
Minimum number of chars (or items for arrays)
- $max : int
-
Maximum number of chars (or items for arrays)
Return values
Field —Returns itself to allow chaining
range()
Adds a range rule
public
range(float $min, float $max) : Field
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.
Does not support array values (e.g. multi-select) — the comparison
($value < $min) expects a scalar.
Parameters
- $min : float
-
Min allowed value
- $max : float
-
Max allowed value
Return values
Field —Returns itself to allow chaining
regex()
Adds a regular expression rule to the field
public
regex(string $expression[, array<string|int, string> $exceptions = [] ]) : Field
For array-valued fields (e.g. multi-select) the regex is applied
per item; the field fails as soon as any single item fails.
Parameters
- $expression : string
-
The regex expression
- $exceptions : array<string|int, string> = []
-
An array of characters to be excluded from the regex
Return values
Field —Returns itself to allow chaining
required()
Adds a required rule
public
required() : Field
With this rule an error is created when no input for this field is given.
Return values
Field —Returns itself to allow chaining
unique()
Adds a unique rule
public
unique(string $table, string $field[, string $ignoreField = null ][, string $ignoreValue = null ]) : Field
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.
Does not support array values (e.g. multi-select) — the rule
runs a single uniqueness query against a scalar value.
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
Field —Returns itself to allow chaining