繁体   English   中英

动态地将文本框添加到zend_form

[英]dynamicaly adding textboxes to zend_form

我希望这是一个快速回答的问题。 我正在使用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()提供了无效的参数

我是要以一种落后的方式来解决这个问题,还是改变我对这种形式的处理方式?或者我在这里缺少任何东西吗?

提前致谢...

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

在控制器中:

$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
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM