将HTML占位符添加到Zend_Form中

时间:2010-01-13 13:01:50

标签: php zend-framework zend-form

我正在使用此代码将Zend_Form用于申请表:

class Form_ApplicationForm extends Zend_Form
{
    public function init()
    {
        $this->setAction('/application/new')
             ->setMethod('post')
             ->setAttrib('id','application');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Your Name')
             ->setRequired(TRUE)
             ->addValidator('alpha', FALSE, array('allowWhiteSpace' => true));

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email Address')
              ->setRequired(TRUE)
              ->addValidator('EmailAddress');

        $this->addElements(array($name,$email));
    }
}

我正在为此表单添加一个flash文件上传程序(swfupload),需要一个HTML代码段才能正常工作,该代码段如下所示:

<div id="swfupload-control">
    <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
    <input type="button" id="button" />
    <p id="queuestatus" ></p>
    <ol id="log"></ol>
</div>

插入此内容的最佳方式是什么,以便它位于<form>内的某个位置,我正在我的控制器中插入,如下所示:

public function newAction()
{
    $form = new Form_ApplicationForm();
    if($this->_request->isPost()){
        $data = $_POST;
        if($form->isValid($data)){
            /// handle data here
        }else{
            $form->populate($data);
        }
    }
    $this->view->form = $form;
}

有没有办法在Zend_Form中添加占位符或类似内容,还是应该使用装饰器或类似内容来完成?

感谢。

2 个答案:

答案 0 :(得分:3)

最简单的方法是在视图中执行此操作:

通过echo每个元素,您可以打印其html。这是一种方法:

<form id="<?= $this->form->getId() ?>" action="<?= $this->form->getAction() ?>">

<? foreach ($this->form as $element): ?>
<?= $element ?>
<? if ($element->getName() == 'email'): ?>
<div id="swfupload-control">
    <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
    <input type="button" id="button" />
    <p id="queuestatus" ></p>
    <ol id="log"></ol>
</div>
<? endif ?>
<? endforeach ?>

</form>

它的作用是打印所有元素,并在email字段后放置所有内容。如果您在表单中添加或删除字段,此代码将继续有效(当然,如果您删除email字段,则会失败。)

答案 1 :(得分:3)

你必须为此编写自己的元素,例如My_Form_Element_SwfUpload以及渲染器。请参阅以下教程:

相关问题