FormHelper
in package
Helper class for generating HTML form attributes.
Provides methods for generating checked/selected attributes and other common form-related HTML generation.
Tags
Table of Contents
Methods
- buildOption() : string
- Build an HTML option element.
- buildSeparator() : string
- Build an HTML option group separator.
- checkInRequest() : string
- Check if a value exists in a request array parameter.
- csrfField() : string
- Generate a hidden CSRF token field for forms.
- csrfToken() : string
- Get the current CSRF token value.
- getChecked() : string
- Return a checked attribute if the value is truthy.
- getSelected() : string
- Return a selected attribute if $value equals $selval.
Methods
buildOption()
Build an HTML option element.
public
static buildOption(int|string $value, string $label[, int|string|null $selected = null ][, bool $disabled = false ]) : string
Parameters
- $value : int|string
-
Option value
- $label : string
-
Option display text
- $selected : int|string|null = null
-
Currently selected value
- $disabled : bool = false
-
Whether the option is disabled
Return values
string —HTML option element
buildSeparator()
Build an HTML option group separator.
public
static buildSeparator([string $label = '--------' ]) : string
Parameters
- $label : string = '--------'
-
Optional label for the separator
Return values
string —HTML disabled option element acting as separator
checkInRequest()
Check if a value exists in a request array parameter.
public
static checkInRequest(mixed $val, string $name) : string
Parameters
- $val : mixed
-
Value to look for
- $name : string
-
Key of the request parameter
Return values
string —' checked="checked" ' if found, ' ' otherwise
csrfField()
Generate a hidden CSRF token field for forms.
public
static csrfField() : string
This method should be called within every form that uses POST, PUT, DELETE, or PATCH methods for CSRF protection.
Tags
Return values
string —HTML hidden input element with CSRF token
csrfToken()
Get the current CSRF token value.
public
static csrfToken() : string
Useful for AJAX requests that need to send the token in a header.
Return values
string —The CSRF token
getChecked()
Return a checked attribute if the value is truthy.
public
static getChecked(mixed $value) : string
Parameters
- $value : mixed
-
Some value that can be evaluated as a boolean
Return values
string —' checked="checked" ' if value is true, '' otherwise
getSelected()
Return a selected attribute if $value equals $selval.
public
static getSelected(int|string|null $value, int|string $selval) : string
Parameters
- $value : int|string|null
-
Current value
- $selval : int|string
-
Value to compare against
Return values
string —' selected="selected" ' if values match, '' otherwise