2009年2月24日星期二

Re: [fw-mvc] problem with outputting tr's on element decorators

-- water <zflist@yahoo.com> wrote
(on Tuesday, 24 February 2009, 12:41 PM -0800):
> I'm trying to create a simple form using the table 'recipe' from the following
> devzone article(http://devzone.zend.com/article/
> 3450-Decorators-with-Zend_Form). I'm having problems with rendering the <tr>'s
> in element_decorator associated elements. I'm am using ZF 1.7.0. Am I doing
> something wrong. I have included the class code and the outputted form. Thanks
> for any help!

I found several issues with the code you attached.

First off, it's better to override the init() method than the
__construct() method. As it was, the signature you had for __construct()
immediately raised an error as it did not follow that used by Zend_Form.

Second, when using decorator aliasing (e.g., array('data' =>
'HtmlTag')), you should use an assoc array for the decorator, with the
keys "decorator" and "options"; this methodology eliminates errors due
to argument order.

I've reworked the class you attached as follows, and it works perfectly:

class AdRefreshForm extends Zend_Form
{

public function init()
{
$this->setAction('/fadmin/app/ad/manage-refresh');
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table')),
'Form',
));

$element_decorator=array(
'ViewHelper',
'Errors',
array('decorator' => array('data' => 'HtmlTag'), 'options' => array('tag' => 'td', 'class' => 'element')),
array('Label', array('tag' => 'td')),
array('decorator' => array('row' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
);


$button_decorator=array(
'ViewHelper',
array('decorator' => array('data' => 'HtmlTag'), 'options' => array('tag' => 'td', 'class' => 'element')),
array('decorator' => array('label' => 'HtmlTag'), 'options' => array('tag' => 'td', 'placement' => 'prepend')),
array('decorator' => array('row' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
);

$this->addElement('text', 'username', array(
'decorators'=>$element_decorator,
'label'=>'Username:',
));

$this->addElement('text', 'firstname', array(
'decorators'=>$element_decorator,
'label'=>'First Name:',
));

$this->addElement('text', 'lastname', array(
'decorators'=>$element_decorator,
'label'=>'Last Name:',
));

$this->addElement('submit', 'save', array(
'decorators'=>$button_decorator,
'label'=>'Save',
));


$this->addElement('submit', 'submit', array(
'label' => 'Update Me',
'decorators' => $button_decorator,
));
}
}

> class AdRefreshForm extends Zend_Form{
>
> public function __construct($channels){
>
> parent::__construct();
>
> $this->setAction('/fadmin/app/ad/manage-refresh');
> $this->setDecorators(array(
> 'FormElements',
> array('HtmlTag', array('tag' => 'table')),
> 'Form',
> ));
>
> $element_decorator=array(
> 'ViewHelper',
> 'Errors',
> array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'
> => 'element')),
> array('Label', array('tag' => 'td'),
> array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
> ));
>
>
> $button_decorator=array(
> 'ViewHelper',
> array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' =>
> 'element')),
> array(array('label' => 'HtmlTag'), array('tag' => 'td', 'placement'
> => 'prepend')),
> array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
> );
>
>
> $this->addElement('text', 'username', array(
> 'decorators'=>$element_decorator,
> 'label'=>'Username:',
> ));
>
> $this->addElement('text', 'firstname', array(
> 'decorators'=>$element_decorator,
> 'label'=>'First Name:',
> ));
>
> $this->addElement('text', 'lastname', array(
> 'decorators'=>$element_decorator,
> 'label'=>'Last Name:',
> ));
> $this->addElement('submit', 'save', array(
> 'decorators'=>$button_decorator,
> 'label'=>'Save',
> ));
>
>
> $submit=new Zend_Form_Element_Submit('submit');
> $submit->setLabel('Update Me');
> $submit->setDecorators($button_decorator);
>
> $this->addElement($submit);
> return $this;
>
>
> }
>
> It is generating the following:
>
> <form enctype="application/x-www-form-urlencoded" action="/fadmin/app/ad/
> manage-refresh" method="post">
> <table>
> <td><label for="username" class="optional">Username:</label></td>
> <td class="element"><input type="text" name="username" id="username" value=
> ""></td>
> <td><label for="firstname" class="optional">First Name:</label></td>
> <td class="element"><input type="text" name="firstname" id="firstname"
> value=""></td>
> <td><label for="lastname" class="optional">Last Name:</label></td>
> <td class="element"><input type="text" name="lastname" id="lastname" value=
> ""></td>
> <tr><td></td><td class="element"><input type="submit" name="save" id="save"
> value="Save"></td></tr>
> <tr><td></td><td class="element"><input type="submit" name="submit" id=
> "submit" value="Update Me"></td></tr>
> </table>
> </form>
>

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: