yiisoft/form

The package helps with implementing data entry forms.

1.2.0 2024-10-09 13:17 UTC

README

Yii Form


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge static analysis type-coverage psalm-level

The package provides a set of widgets to help with dynamic server-side generation of HTML forms. The following widgets are available out of the box:

  • input fields: Checkbox, CheckboxList, Date, DateTimeLocal, Email, File, Hidden, Image, Number, Password, RadioList, Range, Select, Telephone, Text, Textarea, Time, Url;
  • buttons: Button, ResetButton, SubmitButton;
  • group widgets: ButtonGroup, Fieldset.
  • other: ErrorSummary.

These themes are available out of the box:

  • Bootstrap 5 Horizontal,
  • Bootstrap 5 Vertical.

Requirements

  • PHP 8.1 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/form

General usage

Configure themes (optional):

use Yiisoft\Form\ThemeContainer;
use Yiisoft\Form\ThemePath;

ThemeContainer::initialize(
    config: [
        'vertical' => require ThemePath::BOOTSTRAP5_VERTICAL,
        'horizontal' => require ThemePath::BOOTSTRAP5_HORIZONTAL,
    ],
    defaultConfig: 'vertical',
);

... and use Field helper to create widgets:

use Yiisoft\Form\PureField\Field;

echo Field::text('firstName', theme: 'horizontal')->label('First Name')->autofocus();
echo Field::text('lastName', theme: 'horizontal')->label('Last Name');
echo Field::select('sex')->label('Sex')->optionsData(['m' => 'Male', 'f' => 'Female'])->prompt('');
echo Field::number('age')->label('Age')->hint('Please enter your age.');
echo Field::submitButton('Submit')->buttonClass('primary');

The result of executing the code above will be:

<div class="mb-3 row">
    <label class="col-sm-2 col-form-label">First Name</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" name="firstName" autofocus>
    </div>
</div>
<div class="mb-3 row">
    <label class="col-sm-2 col-form-label">Last Name</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" name="lastName">
    </div>
</div>
<div class="mb-3">
    <label class="form-label">Sex</label>
    <select class="form-select" name="sex">
        <option value></option>
        <option value="m">Male</option>
        <option value="f">Female</option>
    </select>
</div>
<div class="mb-3">
    <label class="form-label">Age</label>
    <input type="number" class="form-control" name="age">
    <div class="form-text">Please enter your age.</div>
</div>
<div class="mb-3">
    <button type="submit" class="primary">Submit</button>
</div>

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

License

The Yii Form is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.