Form Controls

class Atk4\Ui\Form\Control

Agile UI dedicates a separate namespace for the Form Controls. Those are quite simple components that present themselves as input controls: line, select, checkbox.

Relationship with Form

All Form Control Decorators can be integrated with Atk4\Ui\Form which will facilitate collection and processing of data in a form. Form Control decorators can also be used as stand-alone controls.

Stand-alone use

Atk4\Ui\Form\Control::set()
Atk4\Ui\Form\Control::jsInput()

Add any form control to your application like this:

$control = Line::addTo($app);

You can set default value and interact with a form control using JavaScript:

$control->set('hello world');


$button = \Atk4\Ui\Button::addTo($app, ['check value']);
$button->on('click', new \Atk4\Ui\Js\JsExpression('alert(\'control value is: \' + [])', [$control->jsInput()->val()]));

When used stand-alone, FormControls will produce a basic HTML (I have omitted id=):

<div class="ui input">
    <input name="line" type="text" placeholder="" value="hello world">
</div>

Using in-form

Form Control can also be used inside a form like this:

$form = \Atk4\Ui\Form::addTo($app);
$control = $form->addControl('name', new \Atk4\Ui\Form\Control\Line());

If you execute this example, you’ll notice that Field now has a label, it uses full width of the page and the following HTML is now produced:

<div class="field">
    <label for="atk_admin_form_generic_name_input">Name</label>
    <div id="atk_admin_form_generic_name" class="ui input">
        <input name="name" type="text" placeholder="" id="atk_admin_form_generic_name_input" value="">
    </div>
</div>

The markup that surronds the button which includes Label and formatting is produced by Atk4\Ui\Form\Layout, which does draw some of the information from the Form Control itself.

Using in Form Layouts

Form may have multiple Form Layouts and that’s very useful if you need to split up form into multiple Tabs or detach form control groups or even create nested layouts:

$form = \Atk4\Ui\Form::addTo($app);
$tabs = \Atk4\Ui\Tabs::addTo($form, [], ['AboveControls']);
\Atk4\Ui\View::addTo($form, ['ui' => 'divider'], ['AboveControls']);

$formPage = Form\Layout::addTo($tabs->addTab('Basic Info'), ['form' => $form]);
$formPage->addControl('name', new \Atk4\Ui\Form\Control\Line());

$formPage = Form\Layout::addTo($tabs->addTab('Other Info'), ['form' => $form]);
$formPage->addControl('age', new \Atk4\Ui\Form\Control\Line());

$form->onSubmit(function (Form $form) {
    return $form->model->get('name') . ' has age ' . $form->model->get('age');
});

This is further explained in documentation for Atk4\Ui\Form\Layout class, however if you do plan on adding your own form control types, it’s important that you extend it properly:

  • Generic (abstract, extends View) - Use this if form control is NOT based on <input>
  • Input (abstract, extends Generic) - Easiest since it already implements <input> and various ways to attach button to the input with markup of Fomantic-UI form control.

Hints

property Atk4\Ui\Form\Control::$hint

When Form Control appears in a Form, then you can specify a Hint also. It appears below the form control and although it intends to be “extra info” or “extra help” due to current limitation of Fomantic-UI the only way we can display hint is using a gray bubble. In the future version of Agile UI we will update to use a more suitable form control.

Hint can be specified either inside Form Control decorator seed or inside the Field::ui attribute:

$form->addControl('title', [], ['values' => ['Mr', 'Mrs', 'Miss'], 'hint' => 'select one']);

$form->addControl('name', ['hint' => 'Full Name Only']);

Text will have HTML characters escaped. You may also specify hint value as an object:

$form->addControl('name', ['hint' => new \Atk4\Ui\Text(
    'Click <a href="https://example.com/" target="_blank">here</a>'
)]);

or you can inject a view with a custom template:

$form->addControl('name', ['hint' => ['template' => new \Atk4\Ui\Template(
    'Click <a href="https://example.com/" target="_blank">here</a>'
)]]);

Read only and disabled form controls

property Atk4\Ui\Form\Control::$readOnly

Read only form controls can be seen in form, can be focused and will be submitted, but we don’t allow to change their value.

property Atk4\Ui\Form\Control::$disabled

Disabled form controls can be seen in form, cannot be focused and will not be submitted. And of course we don’t allow to change their value. Disabled form controls are used for read only model fields for example.

Relationship with Model

In the examples above, we looked at how to create Form Control Decorator object explicitly. The most common use-case in large application is the use with Models. You would need a model, such as Country model as well as Persistence $db:

class Country extends \Atk4\Data\Model
{
    public $table = 'country';

    protected function init(): void
    {
        parent::init();

        $this->addField('name', ['actual' => 'nicename', 'required' => true, 'type' => 'string']);
        $this->addField('sys_name', ['actual' => 'name', 'system' => true]);

        $this->addField('iso', ['caption' => 'ISO', 'required' => true, 'type' => 'string']);
        $this->addField('iso3', ['caption' => 'ISO3', 'required' => true, 'type' => 'string']);
        $this->addField('numcode', ['caption' => 'ISO Numeric Code', 'type' => 'integer', 'required' => true]);
        $this->addField('phonecode', ['caption' => 'Phone Prefix', 'type' => 'integer']);
    }
}

To create a form, the following is sufficient:

$form = \Atk4\Ui\Form::addTo($app);
$form->setModel(new Country($db);

The above will populate fields from model into the form automatically. You can use second argument to Atk4UiForm::setModel() to indicate which fields to display or rely on field_visibility.

When Form controls are populated, then Atk4UiForm::controlFactory is consulted to make a decision on how to translate Model Field into Form Control Decorator.

The rules are rather straightforward but may change in future versions of Agile UI:

  • if enum is defined, use Dropdown
  • consult Atk4UiForm::$typeToDecorator property for type-to-seed association
  • type=password will use Password

You always have an option to explicitly specify which field you would like to use:

$model->addField('long_text', ['ui' => ['rorm' => \Atk4\Ui\Form\Control\TextArea::class]]);

It is recommended however, that you use type when possible, because types will be universally supported by all components:

$model->addField('long_text', ['type' => 'text']);

Note

All forms will be associated with a model. If form is not explicitly linked with a model, it will create a ProxyModel and all form controls will be created automatically in that model. As a result, all Form Control Decorators will be linked with Model Fields.

Line Input Form control

class Atk4\Ui\Form\Control\Input

Implements View for presenting Input form controls. Based around https://fomantic-ui.com/elements/input.html.

Similar to other views, Input has various properties that you can specify directly or inject through constructor. Those properties will affect the look of the input element. For example, icon property:

property Atk4\Ui\Form\Control\Input::$icon
property Atk4\Ui\Form\Control\Input::$iconLeft

Adds icon into the input form control. Default - icon will appear on the right, while leftIcon will display icon on the left.

Here are few ways to specify icon to an Input/Line:

// compact
Line::addTo($page, ['icon' => 'search']);

// Type-hinting friendly
$line = new \Atk4\Ui\Form\Control\Line();
$line->icon = 'search';
$page->add($line);

// using class factory
Line::addTo($page, ['icon' => 'search']);

The ‘icon’ property can be either string or a View. The string is for convenience and will be automatically substituted with new Icon($icon). If you wish to be more specific and pass some arguments to the icon, there are two options:

// compact
$line->icon = ['search', 'class.big' => true];

// Type-hinting friendly
$line->icon = new Icon('search');
$line->icon->addClass('big');

To see how Icon interprets new Icon([‘search’, ‘class.big’ => true]), refer to Icon.

Note

View’s constructor will map received arguments into object properties, if they are defined or addClass() if not. See View::setProperties.

property Atk4\Ui\Form\Control\Input::$placeholder

Will set placeholder property.

property Atk4\Ui\Form\Control\Input::$loading

Set to “left” or “right” to display spinning loading indicator.

property Atk4\Ui\Form\Control\Input::$label
property Atk4\Ui\Form\Control\Input::$labelRight

Convert text into Label and insert it into the form control.

property Atk4\Ui\Form\Control\Input::$action
property Atk4\Ui\Form\Control\Input::$actionLeft

Convert text into Button and insert it into the form control.

To see various examples of form controls and their attributes see demos/form-control/.

Integration with Form

When you use form::addControl() it will create ‘Form Control Decorator’

JavaScript on Input

Atk4\Ui\Form\Control\Input::jsInput([$event[, $otherChain]])

Input class implements method jsInput which is identical to View::js, except that it would target the INPUT element rather then the whole form control:

$control->jsInput(true)->val(123);

onChange event

Atk4\Ui\Form\Control\Input::onChange($expression)

It’s preferable to use this short-hand version of on(‘change’, ‘input’, $expression) method. $expression argument can be JS expression or PHP callback function.

// simple string $f1 = $form->addControl(‘f1’); $f1->onChange(Atk4UiJsJsExpression(‘console.log(‘f1 changed’)’));

// callback $f2 = $form->addControl(‘f2’); $f2->onChange(function () {

return new Atk4UiJsJsExpression(‘console.log(‘f2 changed’)’);

});

// Calendar form control - wraps in function call with arguments date, text and mode $c1 = $form->addControl(‘c1’, new Atk4UiFormControlCalendar([‘type’ => ‘date’])); $c1->onChange(Atk4UiJsJsExpression(‘console.log(‘c1 changed: ‘ + date + ‘, ‘ + text + ‘, ‘ + mode)’));

Lookup

class Atk4\Ui\Form\Control\Lookup

Lookup input is also based on Fomantic-UI dropdown module but with ability to dynamically request server for data it’s data value.

When clicking on a Lookup form control, it will send a query to server and start building it’s list value. Typing into the input form control will reload list value according to search criteria.