Form
in package
Bootstrap-styled form generator with support for various input types and validation.
This class provides methods to generate form elements with proper Bootstrap 5 markup. It includes support for all standard form controls, validation states, and can be extended using hooks. For rapid form generation from objects, see the ObjectToForm class.
Tags
Table of Contents
Methods
- attr() : string
- Converts an associative array of attributes to HTML attributes string
- checkbox() : string|void
- Renders a single checkbox input with Bootstrap styling
- checkboxes() : string|void
- Renders a group of checkboxes with Bootstrap styling
- input() : string|void
- Renders a form input field with Bootstrap 5 styling
- radio() : string|void
- Renders a single radio button with Bootstrap styling
- radios() : string|void
- Renders a group of radio buttons
- select() : string|void
- Renders a Bootstrap-styled select dropdown
- textarea() : string|void
- Renders a Bootstrap-styled textarea field
Methods
attr()
Converts an associative array of attributes to HTML attributes string
public
static attr(array<string|int, mixed> $options) : string
This internal method handles the conversion of PHP array options to proper HTML attributes. It's used by all form element methods to generate the HTML attributes string.
Parameters
- $options : array<string|int, mixed>
-
Associative array of HTML attributes
Tags
Return values
string —HTML attributes as a string
checkbox()
Renders a single checkbox input with Bootstrap styling
public
static checkbox(string $name, string $label, string $value[, bool $is_checked = false ][, array<string|int, mixed> $options = [] ][, bool $return = false ]) : string|void
Parameters
- $name : string
-
The name attribute of the checkbox
- $label : string
-
The label text displayed next to the checkbox
- $value : string
-
The value submitted when the checkbox is checked
- $is_checked : bool = false
-
Whether the checkbox should be checked by default
- $options : array<string|int, mixed> = []
-
Additional HTML attributes and options:
- 'class' => string Additional CSS classes
- 'required' => bool Whether the checkbox is required
- 'invalid-feedback' => string Error message for validation
- Standard HTML5 input attributes (disabled, readonly, etc.)
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
checkboxes()
Renders a group of checkboxes with Bootstrap styling
public
static checkboxes(string $name, array<string|int, mixed> $list_of_radio[, string|array<string|int, mixed> $selected_value = '' ][, bool $inline = false ][, array<string|int, mixed> $options_group = [] ][, array<string|int, mixed> $options_field = [] ][, bool $return = false ]) : string|void
Parameters
- $name : string
-
The name attribute for all checkboxes in the group (will be suffixed with [] for array submission)
- $list_of_radio : array<string|int, mixed>
-
Associative array of value => label pairs for the checkboxes
- $selected_value : string|array<string|int, mixed> = ''
-
The currently selected value(s)
- $inline : bool = false
-
Whether to display checkboxes inline (default: false)
- $options_group : array<string|int, mixed> = []
-
Options for the checkbox group container:
- 'form-group-class' => string CSS class for the form group div
- 'form-check-class' => string CSS class for individual checkbox containers
- 'label' => string Group label text
- 'invalid-feedback' => string Error message for validation
- $options_field : array<string|int, mixed> = []
-
Options for individual checkboxes
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
input()
Renders a form input field with Bootstrap 5 styling
public
static input(string $type, string $name, string $label[, string $value = '' ][, array<string|int, mixed> $options = array() ][, bool $return = false ]) : string|void
Parameters
- $type : string
-
The input type (text, email, password, number, etc.)
- $name : string
-
The name attribute of the input field
- $label : string
-
The label text for the input field
- $value : string = ''
-
The default value of the input field
- $options : array<string|int, mixed> = array()
-
Additional HTML attributes and options liks:
- 'id' => string Custom ID (auto-generated if not provided)
- 'class' => string Additional CSS classes
- 'required' => bool Whether the field is required
- 'placeholder' => string Placeholder text
- 'invalid-feedback' => string in a div with class "invalid-feedback" Error message for validation
- 'list' => array Options for datalist
- Other standard HTML5 input attributes (min, max, step, pattern, etc.)
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
radio()
Renders a single radio button with Bootstrap styling
public
static radio(string $name, string $label, string $value[, string $selected_value = '' ][, array<string|int, mixed> $options = [] ][, bool $return = false ]) : string|void
Parameters
- $name : string
-
The name attribute of the radio button (should be the same for all radio buttons in a group)
- $label : string
-
The label text displayed next to the radio button
- $value : string
-
The value submitted when this radio is selected
- $selected_value : string = ''
-
The currently selected value in the radio group
- $options : array<string|int, mixed> = []
-
Additional HTML attributes and options:
- 'class' => string Additional CSS classes
- 'required' => bool Whether the radio group is required
- Standard HTML5 input attributes (disabled, readonly, etc.)
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
radios()
Renders a group of radio buttons
public
static radios(string $name, array<string|int, mixed> $list_of_radio[, string $selected_value = '' ][, bool $inline = false ][, array<string|int, mixed> $options_group = [] ][, array<string|int, mixed> $options_field = [] ][, bool $return = false ]) : string|void
Example usage:
$options = [
'1' => 'Option One',
'2' => 'Option Two',
'3' => 'Option Three'
];
$selected = '2';
$inline = true; // Display radios inline
$groupOptions = [
'form-group-class' => 'mb-3',
'label' => 'Select an option',
'invalid-feedback' => 'Please select an option'
];
$fieldOptions = [
'class' => 'custom-radio',
'required' => true
];
echo Form::radios('option', $options, $selected, $inline, $groupOptions, $fieldOptions);
Parameters
- $name : string
-
The name attribute for all radio buttons in the group
- $list_of_radio : array<string|int, mixed>
-
Associative array of value => label pairs for the radio buttons
- $selected_value : string = ''
-
[Optional] The value of the currently selected radio button
- $inline : bool = false
-
[Optional] Whether to display radio buttons inline (default: false)
- $options_group : array<string|int, mixed> = []
-
[Optional] Options for the radio button group container:
- 'form-group-class': CSS class for the form group div
- 'form-check-class': CSS class for individual radio containers
- 'label': Group label text
- 'invalid-feedback': Error message to display when validation fails
- $options_field : array<string|int, mixed> = []
-
[Optional] Options for individual radio buttons
- $return : bool = false
-
[Optional] If true, returns the HTML instead of echoing it
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
select()
Renders a Bootstrap-styled select dropdown
public
static select(string $name, string $label, array<string|int, mixed> $select_options[, string $selected = '' ][, array<string|int, mixed> $options = [] ][, bool $return = false ]) : string|void
Parameters
- $name : string
-
The name attribute of the select element
- $label : string
-
The label text for the select field
- $select_options : array<string|int, mixed>
-
Associative array of options or option groups
- Simple array: ['value1' => 'Label 1', 'value2' => 'Label 2']
- With groups: ['Group 1' => ['v1' => 'Label 1'], 'Group 2' => ['v2' => 'Label 2']]
- $selected : string = ''
-
The value of the selected option (optional)
- $options : array<string|int, mixed> = []
-
Additional HTML attributes and options:
- 'class' => string Additional CSS classes
- 'required' => bool Whether the field is required
- 'multiple' => bool Allow multiple selections
- 'floating' => bool Enable/disable floating label (default: true)
- 'invalid-feedback' => string Error message for validation
- Other standard HTML5 select attributes (size, disabled, etc.)
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it
textarea()
Renders a Bootstrap-styled textarea field
public
static textarea(string $name, string $label[, string $value = '' ][, int $rows = 4 ][, array<string|int, mixed> $options = [] ][, bool $return = false ]) : string|void
Parameters
- $name : string
-
The name attribute of the textarea
- $label : string
-
The label text for the textarea
- $value : string = ''
-
The default value of the textarea
- $rows : int = 4
-
Number of visible text lines (default: 4)
- $options : array<string|int, mixed> = []
-
Additional HTML attributes and options:
- 'class' => string Additional CSS classes
- 'required' => bool Whether the field is required
- 'placeholder' => string Placeholder text
- 'invalid-feedback' => string Error message for validation
- Other standard HTML5 textarea attributes
- $return : bool = false
-
If true, returns the HTML instead of echoing it
Tags
Return values
string|void —Returns HTML if $return is true, otherwise echoes it