0

チェックボックスのラベルと入力を作成する View ヘルパーを作成しました。私が抱えている問題は、チェックボックスの値が無視されることです。

私のフォーム要素は次のようになります:

require_once 'Zend/Form/Element/Xhtml.php';

class Core_Form_Element_Other extends Zend_Form_Element_Xhtml
{

    public $helper = 'formOther';

    public function isValid ($value, $context = null)
    {
        return parent::isValid($value, $context);
    }

    public function getValue()
    {
        return parent::getValue();
    }
}

ビュー ヘルパーは次のとおりです。

class Core_View_Helper_FormOther extends Zend_View_Helper_FormElement
{

    public function formOther($name, $value = null, $attribs = null)
    {
        $labelStyle = '';
        $label = '';
        $checkboxAttrib = array('checked' => 1,'unchecked' => 0);
        $textAttrib = array('size' => 10);

        foreach ($attribs as $k => $v)
        {
            $a = str_replace(array('text-', 'checkbox-'), '', $k);
            if (strpos($k, 'text-') !== false)
            {
                $textAttrib[$a] = $v;
            }
            if (strpos($k, 'checkbox-') !== false)
            {
                $checkboxAttrib[$a] = $v;
            }
        }

        $textValue = '';
        $checkboxValue = $checkboxAttrib['unchecked'];
        if (!empty($value))
        {
            $checkboxValue = $checkboxAttrib['checked'];
            $textValue = $value;
        }

        if (isset($attribs['checkboxLabel']))
        {
            $label = $attribs['checkboxLabel'];
        }
        if (isset($attribs['checkboxLabelStyle']))
        {
            $labelStyle = $attribs['checkboxLabelStyle'];
        }

        return $this->view->formCheckbox($name.'_check', $checkboxValue, null, $checkboxAttrib)
            . ' <label style="'. $labelStyle .'">'. $label .'</label> '
            . $this->view->formText($name, $textValue, $textAttrib);
    }
}

最後に、次のように呼び出されます。

$other = $this->createElement('Other', 'otherElem')
            ->setAttrib('checkboxLabel', 'Other')
            ->setAttrib('checkbox-checked', 'yes')
            ->setAttrib('checkbox-unChecked', 'no')
            ->setAttrib('checkboxLabelStyle', 'font-weight:normal;padding-right:0px;')
            ->setAttrib('text-size', 20)
            ->setDecorators(array(
                'ViewHelper',
                array('Errors', array('class' => 'error small', 'placement' => Zend_Form_Decorator_Abstract::PREPEND)),
                array('htmlTag', array('tag' => 'div', 'class' => 'span-8 last'))
            ));
        $this->_telements[] = $other;
4

1 に答える 1

0

「移入時」とは正確にはどういう意味ですか? チェックボックスがチェックされていないときにフォームの送信時に値が欠落している場合、それはデフォルトの html 動作です。

任意の時点で、チェックボックス自体に「checked="checked"」属性を設定していますか? チェックボックスの値とチェックされているかどうかは別物です。

私の意見では、カスタム デコレータを使用することは、zend フォームのスタイルを設定する最も不適切な方法です。Zend からデフォルトの html を出力し、フォームのラベルを jQuery できれいにします。Zendフォームは多くのことに適していますが、スタイルと配置はそれらの1つではありません.

于 2012-08-02T21:31:48.830 に答える