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

'on populate' 到底是什么意思?如果在未选中复选框的情况下提交表单时缺少您的值,则这是默认的 html 行为。

在任何时候,您是否使用属性'checked="checked"'设置复选框本身?复选框的值和选中状态是不同的东西。

在我看来,使用自定义装饰器是最错误的 Zend 表单样式。从 zend 输出默认的 html,用 jQuery 让你的表单标签漂亮。Zend 形式对很多事情都有好处,但风格和安排不是其中之一。

于 2012-08-02T21:31:48.830 回答