FormHelper
class FormHelper
Contains functions for building form elements of various types.
Methods
Adds name of error classes to div associated with a form field.
Supports ability to create a styled button. Supports ability to have functions for event handlers.
Supports ability to create a styled button and styled surrounding div block. Supports ability to have functions for event handlers".
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.
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.
Returns list of errors.
Renders an HTML div element that surrounds an input of type email.
Renders an error message for a particular form field.
Creates a randomly generated csrf token.
Generates a hidden input element.
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.
Generates options for select.
Generates an HTML output element.
Performs sanitization of values obtained during $_POST.
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 59
static string
button(string $buttonText, array $inputAttrs = [])
Supports ability to create a styled button. Supports ability to have functions for event handlers.
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 82
static string
buttonBlock(string $buttonText, array $inputAttrs = [], array $divAttrs = [])
Supports ability to create a styled button and styled surrounding div block. Supports ability to have functions for event handlers".
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 121
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::checkboxBlockLabelRight('Remember Me', 'remember_me', 'on', $this->login->getRememberMeChecked(), [], ['class' => 'form-group'], $this->displayErrors);
Example HTML output is shown below:
at line 183
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 221
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 234
static string
csrfInput()
A hidden input to represent the csrf token in a web form.
Example HTML output is shown below:
at line 247
static string
displayErrors(ArraySet $errors)
Returns list of errors.
at line 297
static string
emailBlock(string $label, string $name, mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
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 330
static string
errorMsg(array $errors, string $name)
Renders an error message for a particular form field.
at line 346
static string
generateToken()
Creates a randomly generated csrf token.
at line 367
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 418
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 447
static string
optionsForSelect(array $options, string|int|null $selectedValue)
Generates options for select.
at line 470
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 481
static array
posted_values(array $post)
Performs sanitization of values obtained during $_POST.
at line 514
static string
radioInput(string $label, string $id, 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.
An example function call is shown below: FormHelper::radioInput('HTML', 'html', 'fav_language', "HTML", $check1, ['class' => 'form-group mr-1']); FormHelper::radioInput('CSS', 'css', 'fav_language', "CSS", $check2, ['class' => 'form-group mr-1']);
Example HTML output is shown below:
at line 533
static string|array
sanitize(string|array $dirty)
Sanitizes potentially harmful string of characters.
at line 570
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("Test", "test", $_POST["test"],['A' => 'a','B' => 'b', 'C' => 'c'], ['class' => 'form-control'], ['class' => 'form-group'], $this->displayErrors);
Example HTML output is shown below:
at line 597
static string
stringifyAttrs(array $attrs)
Stringify attributes.
at line 624
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 656
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 680
static string
telBlock(string $label, string $name, mixed $value = '', array $inputAttrs = [], array $divAttrs = [], array $errors = [])
Renders an HTML div element that surrounds an input of type tel.
at line 729
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: