0

我希望这是一个快速回答的问题。我正在使用 开发一个表单Zend_Form,我有一些Zend_Dojo_Form_Element_Textboxs动态添加到这个表单中。

这些是从数据库中的行添加的,例如

$count = 0;
            //we now loop through the skill types and add them to the form.
            foreach($skillResult as $skill){

                $skillTextBox = new Zend_Dojo_Form_Element_ValidationTextBox('skill-'.$count,
                    array('trim' => true,
                        'NotEmpty' => true,
                        'invalidMessage' => 'This can not be blank'
                    )
                );
                $skillTextBox->addValidator('NotEmpty')
                    ->removeDecorator('DtDdWrapper')
                    ->removeDecorator('HtmlTag')
                    ->removeDecorator('Label');

                //add the element to the form.
                $myForm->addElement($skillTextBox);

                $count++;

            }

然后表单显示在一个视图脚本中,但是我需要提取它。由于我不知道表单中存在多少“技能”文本框,我不确定如何循环并将它们添加到视图脚本中。我通常会通过以下方式将它们添加到 viewScript 中:

<?php foreach($this->element->getElement('skill') as skill) :?>
  <tr>
   <td><?php echo $skill;?></td>
  </tr>
<?php endforeach;?>

但是我收到警告错误消息:为 foreach() 提供的参数无效

我是以一种倒退的方式来解决这个问题并改变我对这种形式的处理方式,还是我在这里遗漏了什么?

提前致谢...

4

1 回答 1

1

如果您在控制器的操作函数中创建表单,您可以执行类似的操作来告诉您的视图脚本您添加了多少技能文本框。

在控制器中:

$this->view->skillTextBoxCount = $count;

鉴于:

// the view is now "this"
$skillCount = $this-skillTextBoxCount;

你也可以这样做:

$elements = $form->getElements();
foreach($elements as $element) {
   if (strpos($element->getName(), 'skill-') === 0) { // must use === here
      // do something with your element
   }
}
于 2009-11-06T03:44:05.227 回答