Documentation

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
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
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

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

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. 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
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.

Parameters
$filter : int

A valid PHP filter

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".

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

Parameters
$min : int

Minimum number of chars

$max : int

Maximum number of chars

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.

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
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.

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

Search results