1.1.3 2023-04-27 09:26 UTC

This package is auto-updated.

Last update: 2024-10-27 12:39:28 UTC


README

hustlahusky/forms

GitHubPackagistInstallationUsage

Installation

Via Composer

$ composer require hustlahusky/forms

Usage

use Hustlahusky\Forms\Builder\FormControlBuilder;
use Hustlahusky\Forms\Handler\FormHandler;
use Hustlahusky\Forms\Form;
use Hustlahusky\Forms\FormControlSize;
use Hustlahusky\Forms\Render;

$form = new Form();

$form->addControl(
    FormControlBuilder::text('first_name', 'First Name')
        ->setRequired()
        ->setSize(FormControlSize::new(6))
        ->build()
);
$form->addControl(
    FormControlBuilder::text('last_name', 'Last Name')
        ->setRequired()
        ->setSize(FormControlSize::new(6))
        ->build()
);
$form->addControl(
    FormControlBuilder::email('email', 'Email')
        ->setRequired()
        ->setSize(FormControlSize::new(6))
        ->build()
);
$form->addControl(
    FormControlBuilder::phone('phone', 'Phone')
        ->setSize(FormControlSize::new(6))
        ->build()
);
$form->addControl(
    FormControlBuilder::radio('radio', 'Radio Buttons')
        ->setReadonly()
        ->addOption('1')
        ->addOption('2')
        ->addOption('3')
        ->setValue('1')
        ->build()
);
$form->addControl(
    FormControlBuilder::select('select')
        ->setReadonly()
        ->addOption('1')
        ->addOption('2')
        ->addOption('3')
        ->setValue('1')
        ->build()
);
$form->addControl(
    FormControlBuilder::select('select_multi')
        ->setReadonly()
        ->setMultiple()
        ->addOption('1')
        ->addOption('2')
        ->addOption('3')
        ->setValue(['1', '2'])
        ->build()
);
$form->addControl(
    FormControlBuilder::checkbox('checkbox')
        ->setReadonly()
        ->setValue(true)
        ->build()
);
$form->addControl(
    FormControlBuilder::checkboxMulti('checkbox_multi')
        ->setReadonly()
        ->addOption('1')
        ->addOption('2')
        ->addOption('3')
        ->setValue(['1', '2'])
        ->build()
);

$render = new Render\BootstrapFormRenderer($form, [
    Render\BootstrapFormRenderer::FORM_ATTRS => [
        'class' => 'w-100',
    ],
    Render\BootstrapFormRenderer::FIELDSET_ATTRS => [
        'class' => 'my-3',
    ],
    Render\BootstrapFormRenderer::LEGEND_ATTRS => [
        'class' => 'h3',
    ],
    Render\BootstrapFormRenderer::ROW_ATTRS => [
        'class' => 'my-2',
    ],
]);

function output(iterable $iter): void
{
    foreach ($iter as $item) {
        echo $item, \PHP_EOL;
    }
}

output($render->startForm());
output($render->renderControls());
output($render->submitButton());
output($render->endForm());


if ('post' === \strtolower($_SERVER['REQUEST_METHOD'])) {
    $handler = new FormHandler($form);
    $data = $handler->handle($_POST);
}

Credits

License

The MIT License (MIT). Please see license file for more information.