PHPackages                             coroq/html-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. coroq/html-form

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

coroq/html-form
===============

v3.1.0(1w ago)0182MITPHPPHP ^8.0CI passing

Since Aug 8Pushed 1w ago1 watchersCompare

[ Source](https://github.com/ozami/coroq-html-form)[ Packagist](https://packagist.org/packages/coroq/html-form)[ RSS](/packages/coroq-html-form/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (6)Versions (26)Used By (0)

coroq/html-form
===============

[](#coroqhtml-form)

Generates HTML form elements from `coroq/form` objects with validation attributes and error handling.

Installation
------------

[](#installation)

```
composer require coroq/html-form
```

Requires PHP 8.0+, `coroq/form` 3.x, and `coroq/html` 1.x or 2.x.

Basic Usage
-----------

[](#basic-usage)

```
use Coroq\Form\Form;
use Coroq\Form\FormItem;
use Coroq\HtmlForm\HtmlForm;
use Coroq\Form\ErrorMessageFormatter;
use Coroq\Form\Error;
use function Coroq\Html\h;

// Create form
$form = new Form();
$form->username = (new FormItem\TextInput())
  ->setMinLength(3)
  ->setMaxLength(20);
$form->email = new FormItem\EmailInput();
$form->age = (new FormItem\IntegerInput())
  ->setMin(18)
  ->setMax(100);

// Create HTML form generator
$formatter = new ErrorMessageFormatter();
$formatter->setMessages([
  Error\EmptyError::class => 'This field is required',
  Error\InvalidEmailError::class => 'Invalid email address',
  Error\Error::class => 'Invalid value', // a base class matches last
]);
$htmlForm = new HtmlForm($form, $formatter);

// Generate inputs
echo h($htmlForm->inputText('username'));
//

echo h($htmlForm->inputEmail('email'));
//

echo h($htmlForm->inputNumber('age'));
//
```

Input Types
-----------

[](#input-types)

```
// Text inputs
$htmlForm->inputText('name');
$htmlForm->inputEmail('email');
$htmlForm->inputPassword('password');
$htmlForm->inputUrl('website');
$htmlForm->inputTel('phone');
$htmlForm->inputNumber('quantity');
$htmlForm->inputDate('birthdate');
$htmlForm->inputHidden('token');
$htmlForm->inputFile('upload');

// Textarea
$htmlForm->textarea('bio');

// Boolean checkbox
$form->agree = new FormItem\BooleanInput();
echo h($htmlForm->inputBoolean('agree'));
//
```

Select and Options
------------------

[](#select-and-options)

```
$form->country = (new FormItem\Select())
  ->setOptions([
    'us' => 'United States',
    'jp' => 'Japan',
    'uk' => 'United Kingdom'
  ])
  ->setValue('jp');

echo h($htmlForm->select('country'));
//
//   United States
//   Japan
//   United Kingdom
//

// Multi-select
$form->colors = (new FormItem\MultiSelect())
  ->setOptions(['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'])
  ->setValue(['r', 'b']);

echo h($htmlForm->select('colors'));
// ...
```

Checkboxes and Radios
---------------------

[](#checkboxes-and-radios)

```
$form->size = (new FormItem\Select())
  ->setOptions(['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'])
  ->setValue('m');

// Individual checkbox
echo h($htmlForm->inputCheckbox('size', 's'));
//

// All checkboxes
foreach ($htmlForm->inputCheckboxes('size') as $value => $checkbox) {
  echo h($checkbox); // Has title attribute with label
}

// Radio buttons
foreach ($htmlForm->inputRadios('size') as $value => $radio) {
  echo h($radio);
}
```

Nested Forms
------------

[](#nested-forms)

```
$form->address = new Form();
$form->address->city = new FormItem\TextInput();
$form->address->postal = new FormItem\TextInput();

echo h($htmlForm->inputText('address/city'));
//

echo h($htmlForm->inputText('address/postal'));
//
```

An item name cannot contain the delimiter, `[` or `]`. Use `setItemPathDelimiter()` to change the delimiter from `/`.

Escaping
--------

[](#escaping)

Every generated element goes through `h()` on the way out, the same as any other value. `h()` returns an `Html` unchanged, so wrapping one costs nothing and the rule stays worth applying without thinking about it:

```
