0

Zend には、2 つのテキスト フィールドとチェックボックスで構成されるカスタム複合要素があります。

これはビュー ヘルパーです。

<?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 に答える