سؤال

Hi there:) i've got a problem with decorators and form which would be in table and in this table want to have also data from database... I dont have any idea how to do this to have a structure like something below, lets say

<table>
<tr>
  <td><?php echo array[0]['name']?>
//and here input from zend form
  <td>
  <select name='foo' id='bar'>
    <option value='something'>Foo</option>
    <option value='something2'>Foo2</option>
  </select>
  </td>
</tr>
</table>

Ofcourse tr will be more and generated with foreach or some loop.

I have something like this:

<?php

class EditArticles_Form_EditArticles extends Zend_Form
{
protected $uid;

public function render()
{


    /* Form Elements & Other Definitions Here ... */
    $this->setName('editarticles');

    $data = new EditArticles_Model_DbTable_EditArticlesModel();
    $datadata = $data->GetArticlesToEdit($this->getUid());    //here is my data from db


    for ($i=0;$i<count($datadata);$i++)
    {           
        $do = new Zend_Form_Element_Select(''.$i);
        $do->addMultiOption('0', 'Aktywny');
                $do->addMultiOption('1', 'Nieaktywny');

        $this->addElements(array($do));
    }



    $submit = new Zend_Form_Element_Submit('updateart');
    $this->addElement($submit);


    //and here are decorators for array, and i would like to have in this table also data from array containing data from database
    $this->addDecorators(array(

                        'FormElements',
                        array('HtmlTag', array('tag' => 'table', 'id' => 'aaaa', 'style' => 'width:500px;')), 'Form', 

                        ));                 

    $this->setElementDecorators(array(

                        'ViewHelper',
                        array(  array('data' => 'HtmlTag'), array('tag' => 'td', 'style' => 'width:200px;')), 
                                array('Label', array('tag' => 'td')),

                        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))

                        ), 
                        //wykluczenie submita z overrida stulu
                        array('submit'), false);


    return  parent::render();

}


//setting user id for get content from db
public function setUid($uid) {
    $this->uid = $uid;
    return $this;
}

public function getUid() {
    return $this->uid;
}

}
?>

output of code above is something like this: (in red marked where i would like to have that selects from form. In this image the table with data is an other table generated in phtml, but i would like to generate that table by form od just insert only the form elements to that table generated in phtml view).

http://img14.imageshack.us/img14/9973/clipboard01pw.png

Something found here: Zend_Form: Database records in HTML table with checkboxes

but i dont know how to start with that...

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top