Skip to content

Creating an model page

zenmiu edited this page May 4, 2012 · 2 revisions

Overview

A link between a model and an editing form is a widget model (\XLite\View\Model\ family). It is used in both the controller when handling data and the form for building its content.

Objective

For example, we are editing the model \XLite\Module\Author\Author\Name\Model\Entity using the controller \XLite\Module\Author\Author\Name\Controller\Admin\EditEntity. We also assume that the form is whole and not divided into sections.

Step 1

Create a widget model class \XLite\Module\Author\Author\Name\View\Model\Entity. It is inherited from \XLite\View\Model\AModel.

Step 1.1

Describe the schema of the form fields, matching them with the fields in the table in the default section. There are two ways to do that:

  1. protected $schemaDefault class property

  2. getFormFieldsForSectionDefault() method

If the fields are static, use the property. If the set of fields may vary depending on various causes, better use the method.

Important note. In both the cases, schema is an associative array, where names of the form fields are the names of the table fields. Otherwise, we would have to reqwite the getModelObjectValue() and setModelProperties() methods.

Example of property:

protected $schemaDefault = array(
    'name' => array(
        self::SCHEMA_CLASS    => '\XLite\View\FormField\Input\Text',
        self::SCHEMA_LABEL    => 'Name',
        self::SCHEMA_REQUIRED => true,
    ),
    'price' => array(
        self::SCHEMA_CLASS    => '\XLite\View\FormField\Input\Text\Integer',
        self::SCHEMA_LABEL    => 'Price',
        self::SCHEMA_REQUIRED => true,
        \XLite\View\FormField\Input\Text\Integer::PARAM_MIN => 0,
    ),
);

Example of method:

/**
* Get form fields for default section
*
* @return array
* @see    ____func_see____
* @since  1.0.13
*/
protected function getFormFieldsForSectionDefault()
{
    $list = array(
        'name' => array(
            self::SCHEMA_CLASS    => '\XLite\View\FormField\Input\Text',
            self::SCHEMA_LABEL    => 'Name',
            self::SCHEMA_REQUIRED => true,
        ),
        'price' => array(
            self::SCHEMA_CLASS    => '\XLite\View\FormField\Input\Text\Integer',
            self::SCHEMA_LABEL    => 'Price',
            self::SCHEMA_REQUIRED => true,
            \XLite\View\FormField\Input\Text\Integer::PARAM_MIN => 0,
        ),
    );

    return $this->getFieldsBySchema($list);
}

Step 1.2

Create method getDefaultModelObject(), which returns either currently edited model object or creates a new object.

Important!

Do not set the new object in the method for writing ( \XLite\Core’Database::getEM()->persist() )!

Step 1.3

Create method getFormClass(), which returns form class name.

Example:

\XLite\Module\Author\Author\Name\View\Form\EditEntity

Step 1.4

Create method getFormButtons() with an array of form buttons. In our case, it would be something like this:

protected function getFormButtons()
{
    $result = parent::getFormButtons();

    $result['submit'] = new \XLite\View\Button\Submit(
        array(
            \XLite\View\Button\AButton::PARAM_LABEL => 'Submit',
            \XLite\View\Button\AButton::PARAM_STYLE => 'action',
        )
    );

    return $result;
}

Step 2

Create a form widget class. In our example:

\XLite\Module\Author\Author\Name\View\Form\EditEntity

In the form, we define its target and action; if we need validation via JS validation engine, override the following method this way:

protected function getDefaultClassName()
{
    $class = parent::getDefaultClassName();
    $class .= ($class ? ' ' : '') . 'validationEngine';
    return $class;
}

If we assume that the form would be used within a tab using the standard \XLite\View’Tabber class, we need to override another method:

protected function getCommonFormParams()
{
    $list = parent::getCommonFormParams();
    $list['page'] = $this->page;
    return $list;
}

Step 3

In our controller template, type in the following:

<widget class="\XLite\Module\Author\Name\View\Model\Entity" useBodyTemplate="1" />

It is assumed that the template already, one way or the other, is displayed in the administrator interface.

Step 4

In the page controller, type in the code that returns form class name. In our case:

protected function getModelFormClass()
{
    return '\XLite\Module\Author\Author\Name\View\Form\EditEntity';
}

Step 5

In the page controller, type in the action method that is in charge of the creation / modification of the model. In our case, by the example of updating model:

public function doActionUpdate()
{
    $this->getModelForm()->performAction('update');
}

Only the following ways of caling the performAction method are available:

  • create - create model
  • update - update model
  • modify - create or update model
  • delete - delete model
Clone this wiki locally