簡體   English   中英

Zend Form View助手

[英]Zend Form View Helper

我已經建立了一個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;

您所說的“人口稠密”到底是什么意思? 如果未選中該復選框,則在提交表單時缺少您的值,這是默認的html行為。

在任何時候,您都使用屬性'checked =“ checked”'來設置復選框嗎? 復選框的值和選中狀態是不同的東西。

在我看來,使用自定義裝飾器是設置zend表單樣式的最錯誤的方法。 從zend輸出默認的html,使用jQuery使您的表單標簽漂亮。 Zend形式對很多事物都有好處,但是樣式和排列方式並不是其中之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM