class FormHelper

Contains functions for building form elements of various types.

Methods

static array
appendErrorClass(array $attrs, array $errors, string $name, string $class)

Adds name of error classes to div associated with a form field.

static string
button(string $buttonText, array $inputAttrs = [])

Supports ability to create a styled button. Supports ability to have functions for event handlers.

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".

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.

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.

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.

static string
csrfInput()

A hidden input to represent the csrf token in a web form.

static string
displayErrors(ArraySet $errors)

Returns list of errors.

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.

static string
errorMsg(array $errors, string $name)

Renders an error message for a particular form field.

static string
generateToken()

Creates a randomly generated csrf token.

static string
hidden(string $name, mixed $value)

Generates a hidden input element.

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.

static string
optionsForSelect(array $options, string|int|null $selectedValue)

Generates options for select.

static string
output(string $name, string $for)

Generates an HTML output element.

static array
posted_values(array $post)

Performs sanitization of values obtained during $_POST.

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.

static string|array
sanitize(string|array $dirty)

Sanitizes potentially harmful string of characters.

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.

static string
stringifyAttrs(array $attrs)

Stringify attributes.

static string
submitBlock(string $buttonText, array $inputAttrs = [], array $divAttrs = [])

Generates a div containing an input of type submit.

static string
submitTag(string $buttonText, array $inputAttrs = [])

Create a input element of type submit.

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.

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.

Details

at line 26
static array appendErrorClass(array $attrs, array $errors, string $name, string $class)

Adds name of error classes to div associated with a form field.

Parameters

array $attrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $errors

The errors array.

string $name

The name of the field associated with this error.

string $class

Name of the class used to identify errors for a form field.

Return Value

array

$attrs Div attributes with error classes added.

at line 60
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:

Parameters

string $buttonText

The contents of the button's label.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

Return Value

string

An HTML button element with its label set and any other optional attributes set.

at line 83
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:

Parameters

string $buttonText

The contents of the button's label.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

Return Value

string

An HTML div surrounding a button element with its label set and any other optional attributes set.

at line 122
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:

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

string $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

bool $checked

The value for the checked attribute. If true this attribute will be set as checked="checked". The default value is false. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and the input element of type checkbox.

at line 184
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:

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

string $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

bool $checked

The value for the checked attribute. If true this attribute will be set as checked="checked". The default value is false. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and the input element.

at line 222
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.

Parameters

string $token

The token string we will test whether or not it exists.

Return Value

bool

The result of the AND operation on whether or not a token exists with a session and if the session's token is equal to the value of the $token parameter.

at line 235
static string csrfInput()

A hidden input to represent the csrf token in a web form.

Example HTML output is shown below:

Return Value

string

The hidden input of type hidden with the generated token set as the value.

at line 248
static string displayErrors(ArraySet $errors)

Returns list of errors.

Parameters

ArraySet $errors

A list of errors and their description that is generated during server side form validation.

Return Value

string

A string representation of a div element containing an input of type checkbox.

at line 298
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:

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

mixed $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and the input element of type email.

at line 331
static string errorMsg(array $errors, string $name)

Renders an error message for a particular form field.

Parameters

array $errors

The error array.

string $name

Used to search errors array for key/form field.

Return Value

string

The error message for a particular field.

at line 347
static string generateToken()

Creates a randomly generated csrf token.

Return Value

string

The randomly generated token.

at line 368
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:

Parameters

string $name

The value for the name and id attributes.

mixed $value

The value for the value attribute.

Return Value

string

The html input element with type hidden.

at line 419
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:

  1. color
  2. date
  3. datetime-local
  4. email
  5. file
  6. month
  7. number
  8. password
  9. range
  10. search
  11. tel
  12. text
  13. time
  14. url
  15. 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:

Parameters

string $type

The input type we want to generate.

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

mixed $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and the input element.

at line 448
static string optionsForSelect(array $options, string|int|null $selectedValue)

Generates options for select.

Parameters

array $options

An array of options for the select.

string|int|null $selectedValue

The currently selected value.

Return Value

string

The HTML element for select with correct value displayed.

at line 471
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:

Parameters

string $name

Sets the value for the name attributes for this input.

string $for

Sets the value for the for attribute.

Return Value

string

The HTML output element.

at line 482
static array posted_values(array $post)

Performs sanitization of values obtained during $_POST.

Parameters

array $post

Values from the $_POST superglobal array when the user submits a form.

Return Value

array

An array of sanitized values from the submitted form.

at line 515
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:


Parameters

string $label

Sets the label for this input.

string $id

The id attribute for the radio input element.

string $name

Sets the value for the name attribute for this input.

string $value

The value we want to set. We can use this to set the value of the value attribute during form validation. It can be set with values during form validation and forms used for editing records.

bool $checked

The value for the checked attribute. If true this attribute will be set as checked="checked". The default value is false. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

Return Value

string

The HTML input element of type radio.

at line 534
static string|array sanitize(string|array $dirty)

Sanitizes potentially harmful string of characters.

Parameters

string|array $dirty

The potentially dirty string.

Return Value

string|array

The sanitized version of the dirty string.

at line 571
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:

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

string|int|null $value

The value we want to set as selected.

array $options

The list of options we will use to populate the select option dropdown. The default value is an empty array.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and option select element.

at line 598
static string stringifyAttrs(array $attrs)

Stringify attributes.

Parameters

array $attrs

The attributes we want to stringify.

Return Value

string

The stringified attributes.

at line 625
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:

Parameters

string $buttonText

Sets the value of the text describing the button.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

Return Value

string

A surrounding div and the input element of type submit.

at line 657
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:

Parameters

string $buttonText

Sets the value of the text describing the button.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

Return Value

string

An input element of type submit.

at line 681
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.

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

mixed $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

The HTML div element surrounding an input of type tel with configuration and values set based on parameters entered during function call.

at line 730
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:

Parameters

string $label

Sets the label for this input.

string $name

Sets the value for the name, for, and id attributes for this input.

string|null $value

The value we want to set. We can use this to set the value of the value attribute during form validation. Default value is the empty string. It can be set with values during form validation and forms used for editing records.

array $inputAttrs

The values used to set the class and other attributes of the input string. The default value is an empty array.

array $divAttrs

The values used to set the class and other attributes of the surrounding div. The default value is an empty array.

array $errors

The errors array. Default value is an empty array.

Return Value

string

A surrounding div and the textarea element.