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 : Request
- $required : array<string|int, mixed>
- A list of the required field names
- $session : Session
- $validators : array<string|int, array<string|int, Validator>>
- 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() : mixed
- Adds fields to the form (merges them with the existing ones)
- addValidator() : mixed
- 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
- fields() : array<string|int, mixed>
- Returns the fields of this form in [name => [field_data]] format
- generateCsrf() : mixed
- If the `$csrf` is true, generates a CSRF field and a CSRF value in the session
- 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 wether a field must be filled or not
- setRequired() : mixed
- 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'
$request
protected
Request
$request
$required
A list of the required field names
protected
array<string|int, mixed>
$required
= []
$session
protected
Session
$session
$validators
Validators for the fields in [name => [validator1, validator2]] format
protected
array<string|int, array<string|int, Validator>>
$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 ]) : mixed
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
mixed —addValidator()
Adds a validator for a field
public
addValidator(string $name, Validator $validator) : mixed
Parameters
- $name : string
-
The name of the field
- $validator : Validator
-
The validator
Return values
mixed —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
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() : mixed
Tags
Return values
mixed —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 wether a field must be filled or not
public
required(string $name) : bool
Parameters
- $name : string
Return values
bool —If true the field must be filled out
setRequired()
Sets a field to be required or not
public
setRequired(string $name, bool $required) : mixed
Parameters
- $name : string
-
The name of the field
- $required : bool
-
Is it required?
Return values
mixed —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 a 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>