ZEND form elements in a table containing also data from the database

大憨熊 提交于 2019-12-01 13:18:29

Several comments:

  1. Typically, adding elements to the form is done in init(), rather than render().

  2. If a consumer object (this is this case, the form) needs a dependency (in this case, the article model) to do its work, it is often helpful to explicitly provide the dependency to the consumer, either in the consumer's constructor or via setter method (ex: $form->setArticleModel($model)). This makes it easier to mock the model when testing the form and clearly illustrates the form's dependence on the model.

  3. Re: rendering other content in the form via decorators: Maybe, take a look at the AnyMarkup decorator. It looks like (sorry, can't fully understand the Polish) you want a select box on each row you output. So, you get your rows using the model, loop through the rows, creating your select box on each row. When you assign decorators to the select element - ViewHelper, Errors, probably an HtmlTag decorator to wrap it in a <td> - you also add the AnyMarkup decorator to prepend the a bunch of <td>'s containing your row data, finally wrapping the whole row in <tr>.

Perhaps something like this (not fully tested, just to give the idea):

class EditArticles_Form_EditArticles extends Zend_Form
{
    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
        parent::__construct();
    }

    public function init()
    {
        $rows = $this->model->GetArticlesToEdit($this->getUid());
        $numRows = count($rows);
        for ($i = 0; $i < $numRows; $i++) {
            $do = new Zend_Form_Element_Select('myselect' . $i);
            $do->addMultiOption('0', 'Aktywny');
            $do->addMultiOption('1', 'Nieaktywny');
            $do->setDecorators(array(
                'ViewHelper',
                array(array('cell' => 'HtmlTag'), array(
                        'tag' => 'td'
                )),
                array('AnyMarkup', array(
                        'markup' => $this->_getMarkupForRow($i, $row),
                        'placement' => 'PREPEND',
                )),
                array(array('row' => 'HtmlTag'), array(
                        'tag' => 'tr'
                )),
            ));
            $this->addElement($do);
        }
    }

    protected function _getMarkupForRow($i, $row)
    {
        return '<td>' . $i . '</td>' .
            '<td>' . $row['nazwa'] . '</td>' .
            '<td>' . $row['typ'] . '</td>' .
            '<td>' . $row['rozmiar'] . '</td>';
    }
}

A final note: Remember to register an element decorator prefix path as follows (in the form, probably in init()):

$this->addElementPrefixPath('My_Decorator', 'My/Decorator', self::DECORATOR);

This allows the element to resolve the short name AnyMarkup into a full classname My_Decorator_AnyMarkup.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!