Describe collections of HTML form inputs
composer require jnjxp/form
$form = (new Jnjxp\Form\FieldFactory)->newFieldCollection();
$form->add('username')
->type('text')
->label('Username')
->attribs(['required' => true])
->help('Enter username or email address');
$form->add('password')
->type('password')
->label('Password')
->attribs(['required' => true]);
$data = $filter->apply($input);
$form->fill($data);
$form->errors($filter->getFailures()->getMessages());
foreach ($form as $field) {
$group = ['form-group'];
$label = ['class' => 'control-label'];
if ($field->id) {
$group[] = 'form-group_' . $field->id;
$label['for'] = $field->id;
}
if ($field->errors) {
$group[] = 'has-errors';
}
echo $helper->tag('div', ['class' => $group]);
if ($field->label) {
echo PHP_EOL;
echo $helper->label($field->label, $label);
}
echo PHP_EOL;
echo $helper->input($field->spec);
if ($field->help) {
echo PHP_EOL;
echo $helper->tag('p', ['class' => 'help-block']);
echo $field->help;
echo $helper->tag('/p');
}
if ($field->errors) {
$errors = $helper->ul(['class' => 'errors']);
$errors->items($field->errors);
echo PHP_EOL;
echo $errors;
echo PHP_EOL;
}
echo $helper->tag('/div') . PHP_EOL;
echo PHP_EOL;
}