0

在 Zend 中,我有一个自定义复合元素,它由两个文本字段和一个复选框组成。

这是视图助手:

<?php
class My_View_Helper_WorkplaceFactor extends Zend_View_Helper_FormElement{

protected $html = '';

public function workplaceFactor($name, $value = null, $attribs = null){
    $this->html = '';
    $factor = $applies = $note = '';
    if($value){
        $factor = $value['factor'];
        $applies = $value['applies'];
        $note = $value['note'];
    }

    $helperText = new Zend_View_Helper_FormText();
    $helperText->setView($this->view);
    $helperCheckbox = new Zend_View_Helper_FormCheckbox();
    $helperCheckbox->setView($this->view);

    $checked = 0;
    if($applies == "1"){
        $checked = 1;
    }

    $this->html .= '<td><label for="' . $name . '[factor]">Faktor</label></td><td>' . $helperText->formText($name . '[factor]', $factor) . '</td>';
    $this->html .= '<td><label for="' . $name . '[applies]">Platí</label></td><td>' . $helperCheckbox->formCheckbox($name . '[applies]', $applies, array('checked' => $checked)) . '</td>';
    $this->html .= '<td><label for="' . $name . '[note]">Poznámka</label></td><td>' . $helperText->formText($name . '[note]', $note) . '</td>';

    return $this->html;
}

}

我的问题是当表单无效并重新加载时填充复选框。当我选中复选框并重新加载时,没关系,它会填充。当我取消选中已选中的选项时,它也可以并在重新加载时正确显示。但是当我想在页面重新加载后选中复选框时,选中的值甚至不在发布请求中,所以它不起作用。

元素的标记如下:

<tr id="factor1">
<td><label for="factor1[factor]">Faktor</label></td><td><input type="text" name="factor1[factor]" id="factor1-factor" value="Prach" /></td>
<td><label for="factor1[applies]">Platí</label></td><td><input type="hidden" name="factor1[applies]" value="0" /><input type="checkbox" name="factor1[applies]" id="factor1-applies" value="0" /></td>
<td><label for="factor1[note]">Poznámka</label></td><td><input type="text" name="factor1[note]" id="factor1-note" value="" /></td></tr>

我在stackoverflow上徘徊,我认为问题在于复选框名称中有[],但如果我不把它们放在那里,“应用”复选框值不属于“因子”值数组,所以它根本不填充。你知道如何找到这个魔法阵的方法吗?

4

1 回答 1

0

好的,在我朋友的帮助和这个答案https://stackoverflow.com/a/9225535/1322246的帮助下,我这样修改了助手:

...
    $checked = isset($value['applies']) && $value['applies'];

    $this->html .= '<td><label for="' . $name . '[factor]">Faktor</label></td><td>' . $helperText->formText($name . '[factor]', $factor) . '</td>';
    $this->html .= '<td><label for="' . $name . '[applies]">Platí</label></td><td>' . $helperCheckbox->formCheckbox($name . '[applies]', $applies, array('value' => 1, 'checked' => $checked), array(1, null)) . '</td>';
    $this->html .= '<td><label for="' . $name . '[note]">Poznámka</label></td><td>' . $helperText->formText($name . '[note]', $note) . '</td>';

...

}

所以换句话说,我必须为 zend 复选框视图助手设置 checkedValue 和 uncheckedValue。现在它起作用了。

于 2012-09-14T11:40:30.710 回答