【问题标题】:Zend framework and Multi-row forms: How do check the next row exists?Zend 框架和多行表单:如何检查下一行是否存在?
【发布时间】:2014-01-29 01:13:29
【问题描述】:

我有一个包含几个常规输入的表单,然后是一个包含很多行的部分。每行包含一组文本输入,例如 rate、quantity、total 等。输入具有 id 和名称,例如 exp_id[0]、exp_id[1]、rate[0]、rate[1]、qty[0] 等. 它类似于电子表格。

我只是在视图文件中制作表单,然后当它提交时,我正在创建一个 Zend_Form 以使用它的验证功能,但我没有呈现 zend 表单。提交表单(在视图文件中创建)时,我想循环遍历一行中每个输入的数组(exp_id[]、rate[]、qty[]、total[] 等)并将一个元素添加到如果每行中的某些元素已填充,则 zend 表单,但我在使用 while 循环遍历行时遇到问题。

if ($this->getRequest()->isPost()) {

    $this->formData = $this->getRequest()->getPost();
    $expRows = array();

    $counter = 0;
    while (array_key_exists ( $this->formData['exp_id'][$counter], $this->formData['exp_id'])) {

    // get required elements
    $rowElements = array();
    $rowElements[] = $this->view->formData['exp_type_id'][$counter]; 
    $rowElements[] = $this->view->formData['total'][$counter];

    // if required elements are not empty then take note of row number for the zend form       
    if (!in_array ("", $rowElements)) {
        $expRows[] = $counter;
    }

    $counter++;

    $invisibleForm = $this->makeForm("newclaim", $expRows);

}

基本上我希望 while 循环检查字段 exp_id[$counter] 是否存在,检查所需字段是否有值,如果是,只需将 $counter 值添加到 $expRows 数组中,该数组将发送到makeForm() 函数。

我已尝试如下更改 while 循环(+ 我现在忘记的其他几个替代方案)

while (isset ($this->formData['exp_id'][$counter])) {

$array = $this->formData['exp_id']; 
while (array_key_exists ($array[$counter], $array)) {

$array = $this->formData['exp_id']; 
while (array_key_exists ($counter, $array)) {

我不确定我只是错过了正确的语法还是完全走错了轨道。

另外,我是 Zend Framework 的新手(使用 1.12)。这是处理这种形式的明智方法吗?有人建议我这样做以简化行的布局。

【问题讨论】:

  • @DragonWarrior 谢谢!

标签: php forms zend-framework


【解决方案1】:

很高兴它有帮助,为了完成问题,我会将其发布为答案,

您应该使用foreach 循环而不是while 循环。

例如来自官方文档,

    <?php foreach ($this->books as $key => $val): ?>
        <tr>
            <td><?php echo $this->escape($val['author']) ?></td>
            <td><?php echo $this->escape($val['title']) ?></td>
        </tr>
        <?php endforeach; ?>

签出, OFFICIAL DOC LINK

this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多