ObjectToForm
in package
Form Generator from Object Definitions
This class provides methods to automatically generate forms from object definitions. It simplifies form creation by converting object properties into appropriate form fields based on their types and validation rules.
Tags
Table of Contents
Methods
- end() : string
- get_input() : string
- Generates the appropriate input field based on field type
- get_token_name() : string
- Generates a token name for a form
- row() : string
- Generates a form row based on a field rule
- start() : string
- Generates the opening HTML for a form
- submit() : string
Methods
end()
public
static end() : string
Return values
stringget_input()
Generates the appropriate input field based on field type
public
static get_input(array<string|int, mixed> $rule, mixed $value) : string
This method creates the actual form input element based on the field type specified in the rule. It handles various input types including text, number, email, select, textarea, checkbox, etc.
Parameters
- $rule : array<string|int, mixed>
-
The field definition rule
- $value : mixed
-
The current value of the field
Tags
Return values
string —The HTML for the input field
get_token_name()
Generates a token name for a form
public
static get_token_name(string $page) : string
This method creates a standardized token name for CSRF protection based on the page name.
Parameters
- $page : string
-
The page identifier
Return values
string —The generated token name
row()
Generates a form row based on a field rule
public
static row(array<string|int, mixed> $rule, mixed $value) : string
This method creates a complete form field with proper wrapping elements based on the field type and rules.
Parameters
- $rule : array<string|int, mixed>
-
The field definition rule
- $value : mixed
-
The current value of the field
Tags
Return values
string —The HTML for the complete form field
start()
Generates the opening HTML for a form
public
static start(string $page[, string $url_success = '' ][, string $url_error = '' ][, string $action_save = 'save' ][, array<string|int, mixed> $attributes = [] ]) : string
This method creates the opening form tag with proper attributes, including CSRF token, action, and redirect URLs.
Parameters
- $page : string
-
The page identifier for the form
- $url_success : string = ''
-
The URL to redirect to on successful submission
- $url_error : string = ''
-
The URL to redirect to on error (defaults to current URL)
- $action_save : string = 'save'
-
The action name for form processing (default: 'save')
- $attributes : array<string|int, mixed> = []
-
Additional form attributes
Tags
Return values
string —The HTML for the form opening tag and hidden fields
submit()
public
static submit([string $button_text = 'Save' ][, array<string|int, mixed> $button_attr = [] ]) : string
Parameters
- $button_text : string = 'Save'
- $button_attr : array<string|int, mixed> = []