PHP code example of hustlahusky / forms
1. Go to this page and download the library: Download hustlahusky/forms library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hustlahusky / forms example snippets
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);
}