2012-08-01 106 views
0

我已經構建了一個View助手,它將構建一個複選框標籤和輸入。我遇到的問題是填充複選框的值被忽略。Zend Form View Helper

我的表單元素看起來像:

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; 

回答

0

究竟你「的意思填充「?如果在未選中複選框時提交表單時缺少值,那是缺省的html行爲。

您是否在任何時候將複選框本身的屬性設置爲checked =「checked」'?複選框和檢查的價值是不同的。

在我看來,使用自定義裝飾器是設計zend表單最方便的方式。輸出來自zend的默認html,使用jQuery讓你的表單標籤更漂亮。 Zend表格適合很多事情,但風格和安排並不是其中之一。

相關問題