在Zend Framework中的表格中表单

时间:2012-08-05 13:37:21

标签: php zend-framework zend-form

我阅读了很多关于如何在zend框架中创建表单的教程,但不幸的是它们都没有为我工作,而且我已经花了很多时间在上面所以我决定问你们。 这是我的代码:

public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'col2')),
        array('Label', array('tag' => 'td','class'=>'taR')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr','class' => 'rowA')),
);

public function init()
{

    $this->setName('message');

    $message_id = new Zend_Form_Element_Hidden('message_id');
    $message_id->addFilter('Int');

    $message_content = new Zend_Form_Element_Text('message_content');
    $message_content->setLabel('Message')
    ->setRequired(true)
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->addValidator('NotEmpty')
    ->setAttrib('class', 'frmtb');
    $username = new Zend_Form_Element_Text('username');
    $username->setLabel('Name')
    ->setRequired(true)
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->addValidator('NotEmpty')
    ->setAttrib('class', 'frmtb');

    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setAttrib('id', 'submitbutton')
    ->setAttrib('class', 'frmbtn');


    //$this->addElements(array($message_id, $username,$message_content, $submit));
    $this->addElement($username,'username',array('decorators' => $this->elementDecorators));
    $this->addElement($message_content,'message_content',array('decorators' => $this->elementDecorators));
    $this->addElement($submit,'submit',array('decorators' => $this->elementDecorators));
}

你能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

好的,所以我很幸运能在发布这个问题后立即找到答案。 This question about Zend form decorators真有帮助。

我的代码现在看起来像这样:

public function init()
{


    $this->setDecorators(array(
            'FormElements',
            array('HtmlTag',array('tag'=>'table')),
            'Form'
    ));

    $this->setName('message');

    $message_id = new Zend_Form_Element_Hidden('message_id');
    $message_id->addFilter('Int');

    $message_content = new Zend_Form_Element_Text('message_content');
    $message_content->setLabel('Message')
    ->setRequired(true)
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->addValidator('NotEmpty')
    ->setAttrib('class', 'frmtb');

    $message_content->setDecorators(array(
            'ViewHelper',
            'Errors',
            array(array('data'=>'HtmlTag'),
                    array('tag'=>'td','class'=>'element')),
            array('Label',array('tag'=>'td')),
            array(array('row'=>'HtmlTag'),array('tag'=>'tr')),

    ));

    $username = new Zend_Form_Element_Text('username');
    $username->setLabel('Name')
    ->setRequired(true)
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->addValidator('NotEmpty')
    ->setAttrib('class', 'frmtb');

    $username->setDecorators(array(
            'ViewHelper',
            'Errors',
            array(array('data'=>'HtmlTag'),
                    array('tag'=>'td','class'=>'element')),
            array('Label',array('tag'=>'td')),
            array(array('row'=>'HtmlTag'),array('tag'=>'tr')),

    ));

    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setAttrib('id', 'submitbutton')
    ->setAttrib('class', 'frmbtn');

    $submit->setDecorators(array(
            'ViewHelper',
            'Errors',
            array(array('data'=>'HtmlTag'),
                    array('tag'=>'td','class'=>'element')),
            array('Label',array('tag'=>'td')),
            array(array('row'=>'HtmlTag'),array('tag'=>'tr')),

    ));


    $this->addElements(array($message_id, $username,$message_content, $submit));
}