PHPackages                             tobento/service-form - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tobento/service-form

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tobento/service-form
====================

Building HTML forms easily.

2.0.1(3mo ago)01922MITPHPPHP &gt;=8.4

Since Mar 4Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-form)[ Packagist](https://packagist.org/packages/tobento/service-form)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-form/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (7)Dependencies (15)Versions (9)Used By (2)

Form Service
============

[](#form-service)

Buliding HTML forms.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Create Form](#create-form)
    - [Form Factory](#form-factory)
    - [Stringable Support](#stringable-support)
    - [Form Elements](#form-elements)
        - [Form](#form)
        - [Input](#input)
            - [Checkbox Type](#checkbox-type)
            - [Radio Type](#radio-type)
        - [Label](#label)
        - [Select](#select)
        - [Textarea](#textarea)
        - [Button](#button)
        - [Fieldset And Legend](#fieldset-and-legend)
        - [Datalist](#datalist)
        - [Option](#option)
    - [Input Data](#input-data)
    - [Tokenizer](#tokenizer)
        - [Session Tokenizer](#session-tokenizer)
        - [Tokenizer Methods](#tokenizer-methods)
        - [Tokenizer PSR-15 Middleware](#tokenizer-psr-15-middleware)
        - [Form Tokenizer Methods](#form-tokenizer-methods)
    - [Messages](#messages)
    - [CSRF Protection](#csrf-protection)
    - [Method Spoofing](#method-spoofing)
    - [Active Form Elements](#active-form-elements)
    - [Form Helper Methods](#form-helper-methods)
    - [JavaScript Validation](#javascript-validation)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the html service project running this command.

```
composer require tobento/service-form

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

Create Form
-----------

[](#create-form)

```
use Tobento\Service\Form\Form;
use Tobento\Service\Form\InputInterface;
use Tobento\Service\Form\TokenizerInterface;
use Tobento\Service\Form\ActiveElementsInterface;
use Tobento\Service\Message\MessagesInterface;

$form = new Form(
    input: null, // null|InputInterface
    tokenizer: null, // null|TokenizerInterface
    activeElements: null, // null|ActiveElementsInterface
    messages: null // null|MessagesInterface
);
```

**Parameters explanation**

ParameterDescription**input**The input data to repopulate values. See [Input Data](#input-data) for more detail.**tokenizer**See [Tokenizer](#tokenizer) for more detail.**activeElements**See [Active Form Elements](#active-form-elements) for more detail.**messages**Used to display messages. See [Messages](#messages) for more detail.Form Factory
------------

[](#form-factory)

You may use form factories to create the form class.

**ResponserFormFactory**

Firstly, make sure you have the responser service installed:

```
composer require tobento/service-responser

```

Check out the [Responser Service](https://github.com/tobento-ch/service-responser) to learn more about it in general.

```
use Tobento\Service\Form\FormFactoryInterface;
use Tobento\Service\Form\ResponserFormFactory;
use Tobento\Service\Form\ActiveElementsInterface;
use Tobento\Service\Form\Form;
use Tobento\Service\Responser\ResponserInterface;

$formFactory = new ResponserFormFactory(
    responser: $responser, // ResponserInterface
    tokenizer: null, // null|TokenizerInterface
    activeElements: null, // null|ActiveElementsInterface
);

var_dump($formFactory instanceof FormFactoryInterface);
// bool(true)

$form = $formFactory->createForm();

var_dump($form instanceof Form);
// bool(true)
```

Stringable Support
------------------

[](#stringable-support)

All methods that output text - such as `label()`, `legend()`, `option()`, `radios()`, `checkboxes()`, and other - accept both:

- `string`
- any object implementing `Stringable` (including `HtmlString`)

This allows you to pass rich, pre-escaped HTML content where appropriate.

**HtmlString**

Use `HtmlString` when you want to output HTML without escaping.

```
use Tobento\Service\Support\HtmlString;

echo $form->label(
    text: new HtmlString('Title'),
    for: 'title'
);
// Title
```

You can also use it in items for radios and checkboxes:

```
use Tobento\Service\Support\HtmlString;

echo $form->checkboxes(
    name: 'colors',
    items: ['red' => new HtmlString('Red')],
    selected: ['blue'],
    attributes: [],
    labelAttributes: [],
);
```

Form Elements
-------------

[](#form-elements)

### Form

[](#form)

```

//

//
```

**form attributes**

```
