Forms in Lists

In the method responsible for generating the list, create a Forms component object:

$form = \tplAdmin::form($this, 'form_template');

The parameters for the component are the controller object and optionally the name of the controller's PHP template.

Here is an example of how to connect a form to a list:

public function listing()
{
    # create a form object
    $form = \tplAdmin::form($this);

    # create form fields
    $form->text('title', 'Title', '', true)
            ->required('Enter the title of the publication')
         ->checkbox('enabled', 'Enabled', true);

    # event on loading record data
    $form->onLoad(function ($id, $fields) {
        # get the necessary data for the edited record
        return $this->model->postData($id, $fields);
    });

    # event on saving record data
    $form->onSave(function ($id, $data) {
        return $this->model->postSave($id, $data);
    });

    # create a list object
    $list = \tplAdmin::blockList($this);

    # attach the form to the list
    $list->formAdd($form);

    // generate the list

    # render the list
    return $list->view();
}

Note that the onLoad() and onSave() event handlers must be set before attaching the form to the list and calling the formAdd() method. For all event handlers, corresponding methods must exist in the module/addon model.

Adding a Popup Form to a List.

This form can be used to view data about a record in a list. Similarly to regular forms, you need to:

  1. Create a form and add event handling:
$popupForm = \tplAdmin::form($this);

# Declare methods for loading and saving data
$popupForm->onLoad(function($id, $fields) {
    # Load data
    return $this->model->postData($id);
})->onSave(function($id, $data) {
    # Save data
});

# Add the required fields
$popupForm
    ->param('titleWidth', 150)
    ->setTitle('Form title')
    # Example field with text preview
    ->staticText('body', 'Text', function() use ($popupForm) {
        $data = $popupForm->_data(); # loaded data in the onLoad method
        return nl2br($data['body']);
    })
    ->buttonsNo(); # hide buttons (if necessary)
  1. Add the form to the list as a custom form with the popup option:
$list->formCustom($popupForm, 'popup_view_action', ['idKey' => 'id', 'popup' => true]);
  1. Add an action in the list that will open this form:
$list->rowsActionAdd('popup_view_action', function($row) use ($list){
    return '<a href="'.$list->urlAjax('popup_view_action', ['id' => $row['id']]).'"
               class="but edit j-form-popup"></a>';
}, 45);