FormHelper
class FormHelper
Contains functions for building form elements of various types, the sanitation, and other setup responsibilities.
Methods
Adds name of error classes to div associated with a form field.
Supports ability to create a styled button.
Supports ability to create a styled button and styled surrounding div block.
Generates a div containing an input of type checkbox with the label to the left.
Generates a div containing an input of type checkbox with the label to the right.
Renders a group of checkboxes sharing one name (submitted as name[]), one wrapping div, and ONE error span. Label-right per box.
Renders a single checkbox input with its label — the bare item only, no wrapping div and no error span (the caller owns the envelope).
Checks if the csrf token exists. This is used to verify that there has been no tampering of a form's csrf token.
A hidden input to represent the csrf token in a web form.
Renders an HTML div element that surrounds an input of type currency.
Assists in the development of forms input blocks with datalist element in forms.
Returns list of errors in the form of an error bag.
Renders an HTML div element that surrounds an input of type email.
Renders an error message for a particular form field.
Renders an HTML div element that surrounds an input of type file.
Formats a raw decimal for display (initial render / after save).
Raw stored value -> display string (grouping + fixed precision).
Creates a randomly generated csrf token.
Generates a hidden input element.
Create a input element of type image.
Generates a div containing an input of type image.
Assists in the development of forms input blocks in forms. It accepts parameters for setting attribute tags in the form section. Not to be used for inputs of type "Submit". For submit inputs use the submitBlock or submitTag functions.
Numeric input (integer or decimal) with optional thousands grouping and fixed precision.
Strips display formatting to a raw decimal string for storage.
Display string -> raw numeric string for storage.
Generates options for select.
Generates an HTML output element.
Performs sanitization of values obtained during $_POST.
Renders a radio button group based on options provided.
Creates an input element of type radio with an accompanying label element. Compatible with radio button groups.
Sanitizes potentially harmful string of characters.
Renders a select element with a list of options.
Stringify attributes.
Generates a div containing an input of type submit.
Create a input element of type submit.
Renders an HTML div element that surrounds an input of type tel.
Assists in the development of textarea in forms. It accepts parameters for setting attribute tags in the form section.
Details
at line 25
static array
appendErrorClass(array $attrs, array $errors, string $name, string $class)
Adds name of error classes to div associated with a form field.
at line 66
static string
button(string $buttonText, array $inputAttrs = [])
Supports ability to create a styled button.
An example function call is shown below:
FormHelper::button(
"Click Me!",
['class' => 'btn btn-large btn-primary', 'onClick' => 'alert(\'Hello World!\')']
);
Example HTML output is shown below:
at line 98
static string
buttonBlock(string $buttonText, array $inputAttrs = [], array $divAttrs = [])
Supports ability to create a styled button and styled surrounding div block.
An example function call is shown below:
FormHelper::buttonBlock(
"Click Me!",
['class' => 'btn btn-large btn-primary', 'onClick' => 'alert(\'Hello World!\')'],
['class' => 'form-group']
);
Example HTML output is shown below:
at line 147
static string
checkboxBlockLabelLeft(string $label, string $name, string $value = "", bool $checked = false, array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Generates a div containing an input of type checkbox with the label to the left.
An example function call is shown below:
FormHelper::checkboxBlockLabelLeft(
'Remember Me',
'remember_me',
'on',
$this->login->getRememberMeChecked(),
[],
['class' => 'form-group'], $this->displayErrors
);
Example HTML output is shown below:
at line 218
static string
checkboxBlockLabelRight(string $label, string $name, string $value = "", bool $checked = false, array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Generates a div containing an input of type checkbox with the label to the right.
An example function call is shown below:
FormHelper::checkboxBlockLabelRight(
'Remember Me',
'remember_me',
'on',
$this->login->getRememberMeChecked(),
[],
['class' => 'form-group mr-1'], $this->displayErrors
);
Example HTML output is shown below:
at line 270
static string
checkboxGroup(string $name, array $options = [], array $selectedValues = [], array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Renders a group of checkboxes sharing one name (submitted as name[]), one wrapping div, and ONE error span. Label-right per box.
Example:
<?= FormHelper::checkboxGroup(
'acls',
$this->acls, // [value => label] map of all ACLs
$this->user->getAcls(), // the set of ACLs this user currently has
[],
['class' => 'form-check'],
$this->displayErrors
); ?>
at line 314
static string
checkboxInput(string $label, string $name, string $value = "", bool $checked = false, array $inputAttrs = [], bool $labelRight = true)
Renders a single checkbox input with its label — the bare item only, no wrapping div and no error span (the caller owns the envelope).
Label sits to the RIGHT of the box by default; pass $labelRight = false to place it on the left.
$inputAttrs is expected already error-classed by the caller (mirrors radioInput).
at line 352
static bool
checkToken(string $token)
Checks if the csrf token exists. This is used to verify that there has been no tampering of a form's csrf token.
at line 362
static string
csrfInput()
A hidden input to represent the csrf token in a web form.
at line 396
static string
currencyBlock(string $label, string $name, string $intlNumberFormat = 'en-US', string $currency = "USD", mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Renders an HTML div element that surrounds an input of type currency.
Example:
<?= FormHelper::currencyBlock(
label: 'Amount',
name: "amount",
inputAttrs: ['class' => 'form-control input-sm'],
divAttrs: ['class' => 'form-group mb-3']
) ?>
at line 462
static string
dataListBlock(string $type, string $label, string $name, string $listName, mixed $value = '', array $options = [], array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Assists in the development of forms input blocks with datalist element in forms.
It accepts parameters for setting attribute tags in the form section.
at line 507
static string
displayErrors(ArraySet $errors)
Returns list of errors in the form of an error bag.
at line 569
static string
emailBlock(string $label, string $name, mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
deprecated
deprecated
Renders an HTML div element that surrounds an input of type email.
An example function call is shown below:
FormHelper::emailBlock(
'Email',
'email',
$this->contact->email,
['class' => 'form-control'],
['class' => 'form-group col-md-6'],
$this->displayErrors
);
Example HTML output is shown below:
at line 597
static string
errorMsg(array $errors, string $name)
Renders an error message for a particular form field.
at line 635
static string
fileBlock(string $label, string $name, array $inputAttrs = [], array $divAttrs = [], bool $multiple = false, array $errors = [])
Renders an HTML div element that surrounds an input of type file.
Multiple File Uploads: Use the $multiple flag to enable multiple file uploads. Name attribute will be formatted correctly and the multiple attribute will be added to the input element.
Example: <?= FormHelper::fileBlock( "Upload Profile Image (Optional)", 'profileImage', ['class' => 'form-control', 'accept' => 'image/gif image/jpeg image/png'], ['class' => 'form-group mb-3']) ?>
at line 674
static string
formatCurrency(mixed $value, string $currency = 'USD', string $locale = 'en-US')
Formats a raw decimal for display (initial render / after save).
Requires ext-intl.
at line 684
static string
formatNumber(mixed $value, int $decimals = 0, bool $useGrouping = false, string $locale = 'en-US')
Raw stored value -> display string (grouping + fixed precision).
at line 698
static string
generateToken()
Creates a randomly generated csrf token.
at line 719
static string
hidden(string $name, mixed $value)
Generates a hidden input element.
An example function call is shown below: FormHelper::hidden("example_name", "example_value");
Example HTML output is shown below:
at line 744
static string
image(string $id, string $src, int $width, int $height, array $inputAttrs = [])
Create a input element of type image.
Example:
<?= FormHelper::image( 'submit', asset('public/logo.png', true), 100, 50, ['class' => 'mt-5 pt-4'] ?>
at line 773
static string
imageBlock(string $id, string $src, int $width, int $height, array $inputAttrs = [], array $divAttrs = [])
Generates a div containing an input of type image.
Example:
<?= FormHelper::imageBlock( 'submit', asset('public/logo.png', true), 100, 50, ['class' => 'mt-5 pt-4'], ['class' => 'text-end']) ?>
at line 836
static string
inputBlock(string $type, string $label, string $name, mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Assists in the development of forms input blocks in forms. It accepts parameters for setting attribute tags in the form section. Not to be used for inputs of type "Submit". For submit inputs use the submitBlock or submitTag functions.
Types of inputs supported:
- color
- date
- datetime-local
- file
- month
- number
- password
- range
- search
- tel
- text
- time
- url
- week
An example function call is shown below: FormHelper::inputBlock('text', 'Example', 'example_name', example_value, ['class' => 'form-control'], ['class' => 'form-group'], $this->displayErrors);
Example HTML output is shown below:
at line 909
static string
number(string $label, string $name, mixed $value = '', array $config = [], array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Numeric input (integer or decimal) with optional thousands grouping and fixed precision.
Config keys (all optional): decimals int Decimal places. 0 = integer. Default 0. useGrouping bool Thousands separators on display. Default false. min int|float|null HTML min. Default null (omitted). max int|float|null HTML max. Default null (omitted). step int|float|string|null HTML step. Default derived from decimals. locale string Intl locale for formatting. Default 'en-US'.
Stores normalized (raw number, no separators); displays formatted.
Examples:
<?= number('Integer', 'int_demo', 42, ['decimals' => 0]); ?>
<?= number('2-decimal, grouped', 'price_demo', 1234.5,
['decimals' => 2, 'useGrouping' => true]); ?>
<?= number('2-decimal, no grouping', 'plain_demo', 1234.5,
['decimals' => 2, 'useGrouping' => false]); ?>
<?= number('3-decimal precision', 'precise_demo', 3.14159,
['decimals' => 3, 'useGrouping' => true]); ?>
<?= number('With min/max', 'bounded_demo', 50,
['decimals' => 0, 'min' => 0, 'max' => 100]); ?>
<?= number('Empty (create mode)', 'empty_demo', '',
['decimals' => 2, 'useGrouping' => true]); ?>
at line 979
static string
normalizeCurrency(mixed $value)
Strips display formatting to a raw decimal string for storage.
Run this in the save handler BEFORE persisting a currency field.
at line 991
static string
normalizeNumber(mixed $value)
Display string -> raw numeric string for storage.
Strips grouping separators and any non-numeric chrome; keeps one decimal point and a leading minus.
at line 1004
static string
optionsForSelect(array $options, string|int|null $selectedValue)
Generates options for select.
at line 1027
static string
output(string $name, string $for)
Generates an HTML output element.
An example function call is shown below: FormHelper::output("my_name", "for_value")
Example HTML output is shown below:
at line 1038
static array
posted_values(array $post)
Performs sanitization of values obtained during $_POST.
at line 1067
static string
radioGroup(string $name, array $options = [], string|int|null $selectedValue = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Renders a radio button group based on options provided.
Example: <?= FormHelper::radioGroup( 'inactive', // name [0 => 'Active', 1 => 'Inactive'], // [value => label] map $this->user->inactive, // selected value: 0 or 1 ['class' => 'form-check-input'], // inputAttrs (applied to every radio) ['class' => 'form-group mb-3'], // divAttrs $this->displayErrors // errors ); ?>
at line 1118
static string
radioInput(string $label, string $name, string $value, bool $checked = false, array $inputAttrs = [])
Creates an input element of type radio with an accompanying label element. Compatible with radio button groups.
Example:
<?= FormHelper::radioInput( "Phone", 'contact', 'phone', false, ['class' => 'form-group mb-3']) ?>
at line 1140
static string|array
sanitize(string|array $dirty)
Sanitizes potentially harmful string of characters.
at line 1174
static string
selectBlock(string $label, string $name, string|int|null $value, array $options, array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Renders a select element with a list of options.
An example function call is shown below: <?= FormHelper::selectBlock( 'Account Status', // label 'inactive', // name — matches the schema column $this->user->inactive, // current value: 0 or 1 [0 => 'Active', 1 => 'Inactive'], // [value => label] map ['class' => 'form-select'], // inputAttrs ['class' => 'form-group mb-3'], // divAttrs $this->displayErrors // errors ); ?>
at line 1201
static string
stringifyAttrs(array $attrs)
Stringify attributes.
at line 1228
static string
submitBlock(string $buttonText, array $inputAttrs = [], array $divAttrs = [])
Generates a div containing an input of type submit.
An example function call is shown below: FormHelper::submitBlock("Save", ['class'=>'btn btn-primary'], ['class'=>'text-end']);
Example HTML output is shown below:
at line 1260
static string
submitTag(string $buttonText, array $inputAttrs = [])
Create a input element of type submit.
An example function call is shown below: FormHelper::submitTag("Save", ['class'=>'btn btn-primary']);
or
self::submitTag("Save", ['class'=>'btn btn-primary']);
Example HTML output is shown below:
at line 1296
static string
telBlock(string $label, string $name, mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
deprecated
deprecated
Renders an HTML div element that surrounds an input of type tel.
Example:
<?= FormHelper::telBlock( 'Home phone', 'phone', $this->user->phone, ['class' => 'form-control input-sm'], ['class' => 'form-group mb-3']) ?>
at line 1350
static string
textareaBlock(string $label, string $name, string|null $value, array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Assists in the development of textarea in forms. It accepts parameters for setting attribute tags in the form section.
An example function call is shown below: FormHelper::textAreaBlock("Example", 'example_name', example_value, ['class' => 'form-control input-sm', 'placeholder' => 'foo'], ['class' => 'form-group'], $this->displayErrors);
Example HTML output is shown below: