jnjxp/form

Describe collections of HTML form inputs

0.3.0 2017-09-06 15:00 UTC

This package is auto-updated.

Last update: 2024-03-26 07:02:21 UTC


README

Describe collections of HTML form inputs

Latest version Build Status Coverage Status Quality Score

Installation

composer require jnjxp/form

Usage

$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;
}