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.0.0(5mo ago)0172[1 PRs](https://github.com/ozami/coroq-html-form/pulls)MITPHPPHP ^8.0CI failing

Since Aug 8Pushed 3mo 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 4d ago

READMEChangelog (10)Dependencies (3)Versions (23)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.0.0, and `coroq/html` 0.4.0.

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

[](#basic-usage)

```
use Coroq\Form\Form;
use Coroq\Form\FormItem;
use Coroq\HtmlForm\HtmlForm;
use Coroq\Form\ErrorMessageFormatter;
use Coroq\Form\BasicErrorMessages;

// 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(BasicErrorMessages::get());
$htmlForm = new HtmlForm($form, $formatter);

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

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

echo $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 $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 $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 $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 $htmlForm->inputCheckbox('size', 's');
//

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

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

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

[](#nested-forms)

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

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

// Array path
echo $htmlForm->inputText(['address', 'postal']);
//
```

Displaying Values
-----------------

[](#displaying-values)

```
$form->price = (new FormItem\NumberInput())->setValue('1234.56');
$form->created = (new FormItem\DateInput())->setValue('2024-01-15');

// Plain value
echo $htmlForm->value('price');
// 1234.56

// Formatted number
echo $htmlForm->number('price', 2, '.', ',');
// 1,234.56

// Formatted date
echo $htmlForm->date('created', 'F d, Y');
// January 15, 2024

// Custom format
echo $htmlForm->format('price', 'Price: $%s');
// Price: $1234.56
```

Error Handling
--------------

[](#error-handling)

```
$form->email = (new FormItem\EmailInput())->setValue('invalid-email');
$form->email->validate();

// Display errors
echo $htmlForm->error('email');
// Invalid email address

// Multiple fields
echo $htmlForm->error(['email', 'username']);

// Check for errors
if ($htmlForm->getItemIn('email')->hasError()) {
    // ...
}
```

Form State Attributes
---------------------

[](#form-state-attributes)

```
// Optional field
$form->nickname = (new FormItem\TextInput())
    ->setRequired(false);

// Disabled field
$form->locked = (new FormItem\TextInput())
    ->setDisabled(true);

// Read-only field
$form->computed = (new FormItem\TextInput())
    ->setReadOnly(true);

echo $htmlForm->inputText('optional');
//

echo $htmlForm->inputText('locked');
//
```

Bootstrap Integration
---------------------

[](#bootstrap-integration)

### Bootstrap 4

[](#bootstrap-4)

```
use Coroq\HtmlForm\Integration\Bootstrap4;

$htmlForm = new Bootstrap4($form, $formatter);

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

echo $htmlForm->select('country');
// ...

// With validation errors
$form->email->validate(); // Fails
echo $htmlForm->inputEmail('email');
//

echo $htmlForm->error('email');
// Invalid email address
```

### Bootstrap 5

[](#bootstrap-5)

```
use Coroq\HtmlForm\Integration\Bootstrap5;

$htmlForm = new Bootstrap5($form, $formatter);

echo $htmlForm->select('country');
// ...
// Note: Bootstrap 5 uses form-select instead of form-control
```

Complete Example
----------------

[](#complete-example)

```
// Setup form
$form = new Form();
$form->username = (new FormItem\TextInput())
    ->setMinLength(3)
    ->setMaxLength(20);
$form->email = new FormItem\EmailInput();
$form->password = (new FormItem\TextInput())
    ->setMinLength(8);
$form->country = (new FormItem\Select())
    ->setOptions(['us' => 'USA', 'jp' => 'Japan', 'uk' => 'UK']);
$form->agree = new FormItem\BooleanInput();

// Handle submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $form->setValue($_POST);
    if ($form->validate()) {
        // Process form
        $data = $form->getValue();
    }
}

// Create HTML generator
$formatter = new ErrorMessageFormatter();
$formatter->setMessages(BasicErrorMessages::get());
$htmlForm = new HtmlForm($form, $formatter);
?>

        Username
