Download the PHP package tobento/service-form without Composer
On this page you can find all versions of the php package tobento/service-form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-form
Form Service
Buliding HTML forms.
Table of Contents
- Getting started
- Requirements
- Highlights
- Documentation
- Create Form
- Form Factory
- Form Elements
- Form
- Input
- Checkbox Type
- Radio Type
- Label
- Select
- Textarea
- Button
- Fieldset And Legend
- Datalist
- Option
- Input Data
- Tokenizer
- Session Tokenizer
- Tokenizer Methods
- Tokenizer PSR-15 Middleware
- Form Tokenizer Methods
- Messages
- CSRF Protection
- Method Spoofing
- Active Form Elements
- Form Helper Methods
- JavaScript Validation
- Credits
Getting started
Add the latest version of the html service project running this command.
Requirements
- PHP 8.0 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
Documentation
Create Form
Parameters explanation
Parameter | Description |
---|---|
input | The input data to repopulate values. See Input Data for more detail. |
tokenizer | See Tokenizer for more detail. |
activeElements | See Active Form Elements for more detail. |
messages | Used to display messages. See Messages for more detail. |
Form Factory
You may use form factories to create the form class.
ResponserFormFactory
Firstly, make sure you have the responser service installed:
Check out the Responser Service to learn more about it in general.
Form Elements
Form
form attributes
Input
Parameters explanation
Parameter | Description |
---|---|
string $name | The name of the input element. |
string $type = 'text' | The input type such as text, hidden, checkbox, ... |
null|string $value = null | Any value. |
array $attributes = [] | Any input element attributes. For instance, ['class' => 'class-name']. |
mixed $selected = null | The selected value(s) for checkbox or radio type. |
bool $withInput = true | If the value should be repopulated with the input data. |
Checkbox Type
checkboxes
You may use the checkboxes method to create multiple checkbox input elements with its labels:
Parameters explanation
Parameter | Description |
---|---|
string $name | The name of the input element. |
iterable $items = [] | The items. |
array $selected = [] | The selected checkbox values. |
array $attributes = [] | Any attributes for the checkbox element. For instance, ['class' => 'class-name'] |
array $labelAttributes = [] | Any attributes for the label element. For instance, ['class' => 'class-name'] |
bool $withInput = true | If the value should be repopulated with the input data. |
string $wrapClass = 'form-wrap-checkbox' | The class for the wrap element. |
checkboxes with each
You may use the each method if you have an array of objects or just to have more control over the value, label and array key for the name of the checkbox.
Radio Type
radios
You may use the radios method to create multiple radio input elements with its labels:
Parameters explanation
Parameter | Description |
---|---|
string $name | The name of the input element. |
iterable $items = [] | The items. |
null|string $selected = null | The selected radio value. |
array $attributes = [] | Any attributes for the radio element. For instance, ['class' => 'class-name'] |
array $labelAttributes = [] | Any attributes for the label element. For instance, ['class' => 'class-name'] |
bool $withInput = true | If the value should be repopulated with the input data. |
string $wrapClass = 'form-wrap-radio' | The class for the wrap element. |
radios with each
You may use the each method if you have an array of objects or just to have more control over the value and label.
Label
Parameters explanation
Parameter | Description |
---|---|
string $text | The label text. |
null|string $for = null | The for attribute should be equal to the id attribute of the related element to bind them together. |
array $attributes = [] | Any attributes for the label element. For instance, ['class' => 'class-name'] |
string $requiredText = '' | Any required text. |
string $optionalText = '' | Any optional text. |
with required text
with optional text
Select
Parameters explanation
Parameter | Description |
---|---|
string $name | The name of the select element. |
iterable $items = [] | The items. Multidimensional array for optgroups. |
mixed $selected = null | The selected value(s). Use array for multiple selected, but do not forget to set the multiple attribute on the select element and the select name should end with '[]'. |
array $selectAttributes = [] | Any attributes for the select element. For instance, ['class' => 'class-name'] |
array $optionAttributes = [] | Any attributes for a single option. For instance, ['red' => ['class' => 'class-name']]. You may use asterix sign for all option elements, ['*' => ['class' => 'class-name']] |
array $optgroupAttributes = [] | Any attributes for the optgroup elements. For instance, ['class' => 'class-name'] |
null|array $emptyOption = null | Adds an empty option with the specified value and label. |
bool $withInput = true | If the value should be repopulated with the input data. |
with optgroup elements
with each method
You may use the each method if you have an array of objects or just to have more control over the value, label and array key for the name of the radio.
with empty option
Textarea
Parameters explanation
Parameter | Description |
---|---|
string $name | The name of the textarea element. |
null|string $value = null | The value if any. |
array $attributes = [] | Any attributes for the textarea element. For instance, ['class' => 'class-name'] |
bool $withInput = true | If the value should be repopulated with the input data. |
Button
Parameters explanation
Parameter | Description |
---|---|
string $text | The text for the button. |
array $attributes = [] | Any attributes for the textarea element. For instance, ['class' => 'class-name'] |
bool $escText = true | True escaping text, otherwise not. |
Fieldset And Legend
Parameters explanation
Parameter | Description |
---|---|
string $legend | The text for legend element. |
array $attributes = [] | Any attributes for the fieldset element. For instance, ['class' => 'class-name'] |
array $legendAttributes = [] | Any attributes for the legend element. For instance, ['class' => 'class-name'] |
Datalist
Parameters explanation
Parameter | Description |
---|---|
string $name | The name (id) of the datalist. |
iterable $items = [] | Any items. |
array $attributes = [] | Any attributes for the datalist element. For instance, ['class' => 'class-name'] |
with each method
You may use the each method if you have an array of objects or just to have more control over the value.
Option
Parameters explanation
Parameter | Description |
---|---|
string $value | The value of the option. |
null|string $text = null | The option text if any. |
array $attributes = [] | Any attributes for the option element. For instance, ['class' => 'class-name'] |
mixed $selected = null | The selected value(s). |
null|string $name = null | If set, it is used as the name for the Active Form Elements. |
Input Data
The input data is used to repopulate the values of the form elements.
Create Input
Now, the value of the form elements gets automatically repopulated if a input data for the specified element exists.
Form Input Methods
You may use the following methods to access the input data on the form class.
getInput
hasInput
withInput
Returns a new Form instance with the specified input.
Tokenizer
The tokenizer is used to generate and verify tokens to protect your application from cross-site request forgeries.
Session Tokenizer
Firstly, make sure you have the session service installed:
Check out the Session Service to learn more about it in general.
Tokenizer Methods
setTokenName
getTokenName
setTokenInputName
getTokenInputName
get
Return the token for the specified name.
generate
Generates and returns a new token for the specified name.
delete
Delete the token for the specified name.
verifyToken
Tokenizer PSR-15 Middleware
You may also use the VerifyCsrfToken::class middleware to verify the form token.\ If token is invalid, a InvalidTokenException will be thrown.
You may wish to exclude a set of URIs from CSRF protection:
Form Tokenizer Methods
tokenizer
generateToken
generateTokenInput
Messages
Messages are used to display any message type on the form elements.
Check out the Message Service to learn more about it in general.
Form Messages Methods
messages
withMessages
Returns a new instance with the specified messages.
getMessage
Returns the message for the specified key.
getRenderedMessageKeys
Returns the rendered message keys.
Adding Messages
An example of how to add messages for a specific form element.
You may check out the Validation Service for validating the form and passing the validator messages to the form.
CSRF Protection
If you have specified a tokenizer on the Form class, the form method will automatically add a hidden input element with the token.
Method Spoofing
If you set PUT, PATCH or DELETE as the method, a hidden input element named _method will be automatically added for spoofing.
Active Form Elements
getActiveElements
Returns the active elements.
isActive
If the element is active.
Form Helper Methods
nameToArray
Generates the specified name to an array string if name contains notation syntax.
nameToNotation
Generates the specified name from an array string to a notation based name.
nameToId
Generates the specified name to a valid id.
hasArrayNotation
Check if the specified name contains a notation.
JavaScript Validation
You may use the validation.js for adding custom messages for your form elements when using validation attributes.
Import File
Validation
Use the attribute to specify messages for specific validation attributes.
required
pattern
min/max/step
minlength/maxlength
mismatch
The mismatch message will be displayed for type mismatches. See ValidityState/typeMismatch
Credits
- Tobias Strub
- All Contributors
All versions of service-form with dependencies
tobento/service-message Version ^1.0
tobento/service-collection Version ^1.0