Documentation

Form
in package

Represents a form

Table of Contents

$csrf  : bool
Is this form uses CSRF?
$errors  : array<string|int, mixed>
The error messages of the fields in [name => message] format
$fields  : array<string|int, mixed>
Holds the fields
$name  : string
Stores the name of the form
$request  : RequestInterface
$required  : array<string|int, mixed>
A list of the required field names
$session  : SessionInterface
$validators  : array<string|int, mixed>
Validators for the fields in [name => [validator1, validator2]] format
$values  : array<string|int, mixed>
The values of the fields in [name => value] format
__construct()  : mixed
Creates the form with given name and `$csrf` value
addError()  : void
Adds an error to the form itself
addFields()  : void
Adds fields to the form (merges them with the existing ones)
addValidator()  : void
Adds a validator for a field
addValues()  : void
Adds the values for the fields (merges them with the existing ones)
bind()  : void
Binds the request values to the field values
csrfName()  : string
Returns with the CSRF field name
csrfSessionName()  : string
Returns with the CSRF session name
error()  : string|null
Returns an error message for a field
fetch()  : string
fetchErrors()  : string
fetchField()  : string
fetchInput()  : string
fields()  : array<string|int, mixed>
Returns the fields of this form in [name => [field_data]] format
generateCsrf()  : void
If the `$csrf` is true, generates a CSRF field and a CSRF value in the session
idByNameAndField()  : string
name()  : string
Returns the name of this form
process()  : bool
Processes a form if the request method is `$httpMethod`, adds the CSRF field if `$csrf` is true
required()  : bool
Returns if a field must be filled or not
setRequired()  : void
Sets a field to be required or not
setValues()  : void
Sets the values for the fields (clears the previous ones)
validate()  : bool
Runs the validators per field if the field is required or has value
validateCsrf()  : bool
Returns true if the CSRF session value equals with the CSRF field value
value()  : null|string
Returns a value for a field
values()  : array<string|int, mixed>
Returns with the values for the fields in [name => value] form

Properties

$csrf

Is this form uses CSRF?

protected bool $csrf = true

$errors

The error messages of the fields in [name => message] format

protected array<string|int, mixed> $errors = []

$fields

Holds the fields

protected array<string|int, mixed> $fields = []

$name

Stores the name of the form

protected string $name = 'form'

$required

A list of the required field names

protected array<string|int, mixed> $required = []

$validators

Validators for the fields in [name => [validator1, validator2]] format

protected array<string|int, mixed> $validators = []

$values

The values of the fields in [name => value] format

protected array<string|int, mixed> $values = []

Methods

__construct()

Creates the form with given name and `$csrf` value

public __construct(Request $request, Session $session[, string $name = 'form' ][, bool $csrf = true ]) : mixed
Parameters
$request : Request

The HTTP request

$session : Session

The session used for the CSRF check

$name : string = 'form'

The name of the form, can be an empty string (usually for filter forms)

$csrf : bool = true

Is the form should use a CSRF field and validate it on process()?

Return values
mixed —

addError()

Adds an error to the form itself

public addError(string $error) : void
Parameters
$error : string
Return values
void —

addFields()

Adds fields to the form (merges them with the existing ones)

public addFields(array<string|int, mixed> $fields[, bool $required = true ]) : void
Parameters
$fields : array<string|int, mixed>

The fields in [name => [field_data]] format

$required : bool = true

Is this field required to be filled out?

Return values
void —

addValidator()

Adds a validator for a field

public addValidator(string $name, AbstractValidator $validator) : void
Parameters
$name : string

The name of the field

$validator : AbstractValidator

The validator

Return values
void —

addValues()

Adds the values for the fields (merges them with the existing ones)

public addValues(array<string|int, mixed> $values) : void
Parameters
$values : array<string|int, mixed>
Return values
void —

bind()

Binds the request values to the field values

public bind() : void

If the form has a name it will use the form_name[] value from the request, otherwise: one field name one request parameter name.

Return values
void —

csrfName()

Returns with the CSRF field name

public csrfName() : string
Return values
string —

csrfSessionName()

Returns with the CSRF session name

public csrfSessionName() : string
Return values
string —

error()

Returns an error message for a field

public error(string $name) : string|null
Parameters
$name : string

The field name

Return values
string|null —

The error message or null

fetch()

public fetch() : string
Return values
string —

fetchErrors()

public fetchErrors() : string
Return values
string —

fetchField()

public fetchField(string $name, array<string|int, mixed> $field) : string
Parameters
$name : string
$field : array<string|int, mixed>
Return values
string —

fetchInput()

public fetchInput(string $name, array<string|int, mixed> $field) : string
Parameters
$name : string
$field : array<string|int, mixed>
Return values
string —

fields()

Returns the fields of this form in [name => [field_data]] format

public fields() : array<string|int, mixed>
Return values
array<string|int, mixed> —

generateCsrf()

If the `$csrf` is true, generates a CSRF field and a CSRF value in the session

public generateCsrf() : void
Tags
throws
MicroException

If it couldn't gather sufficient entropy for random_bytes

Return values
void —

idByNameAndField()

public idByNameAndField(string $name, array<string|int, mixed> $field) : string
Parameters
$name : string
$field : array<string|int, mixed>
Return values
string —

name()

Returns the name of this form

public name() : string
Return values
string —

process()

Processes a form if the request method is `$httpMethod`, adds the CSRF field if `$csrf` is true

public process([string $httpMethod = 'POST' ]) : bool
Parameters
$httpMethod : string = 'POST'

The required HTTP method

Return values
bool —

Returns true if the form is valid

required()

Returns if a field must be filled or not

public required(string $name) : bool
Parameters
$name : string
Return values
bool —

setRequired()

Sets a field to be required or not

public setRequired(string $name, bool $required) : void
Parameters
$name : string

The name of the field

$required : bool

Is it required?

Return values
void —

setValues()

Sets the values for the fields (clears the previous ones)

public setValues(array<string|int, mixed> $values) : void
Parameters
$values : array<string|int, mixed>
Return values
void —

validate()

Runs the validators per field if the field is required or has value

public validate() : bool

If one validator fails for a field the other validators will NOT run for that field.

Return values
bool —

The form validation was successful?

validateCsrf()

Returns true if the CSRF session value equals with the CSRF field value

public validateCsrf() : bool
Return values
bool —

value()

Returns a value for a field

public value(string $name[, bool $escape = false ]) : null|string
Parameters
$name : string

The name of the field

$escape : bool = false

Should the value to be escaped for an HTML attribute?

Return values
null|string —

The value of the field

values()

Returns with the values for the fields in [name => value] form

public values() : array<string|int, mixed>
Return values
array<string|int, mixed> —

Search results