2012-08-01 107 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表格适合很多事情,但风格和安排并不是其中之一。

相关问题