【问题标题】:zend form Issue in zend frameworkzend 框架中的 zend 表单问题
【发布时间】:2012-02-02 19:19:19
【问题描述】:

我正在使用 zend 框架表单, 我是 zend 框架的新手,我想像这样显示我的复选框表单:-

*SK336
  *CP
  *PES
  *JCP
  *BGH
*SK996
  *KO
  *RTY
  *HGR
*SK547
  *GPK
*SK478
  *JUP

注意 :- * 是这里的复选框

我在这里尝试的是:-

public function init()
{

  $parents = array();
     $childs = array();
     foreach ($this->Tagkey as $aResultDataValue) {
                    $parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
                    $childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
    }


    foreach ($parents as $parent){ // print_r ($parents); die();
    $tags = new Zend_Form_SubForm();
    $tags->addElements(array(   
    new Zend_Form_Element_MultiCheckbox('parent', array(
                    'multiOptions' =>   array($parent),
                    'filters'      => array('StringTrim'),
                    'validators'   => array(
                    array('InArray',
                          false,
                          array($parent))
                          )

       )),
       ));
    foreach ($childs as $child){
    $tags->addElements(array(
    new Zend_Form_Element_MultiCheckbox('child', array(
          'multiOptions' => array($child),
          'filters'      => array('StringTrim'),
          'validators'   => array(
                    array('InArray',
                          false,
                          $child)
                          )    
        )),
        ));
    }

    $this->addSubForms(array(
    'tags'  => $tags,
        )
    );
    }

我可以在任何.php 页面中创建这种类型的结构,但现在不能以 zend 框架形式创建,我在这里使用 zend 子形式。

当我使用这个查询时,我现在也遇到了一个错误

警告:htmlspecialchars() 期望参数 1 为字符串,数组在 /var/www/dashboard_campaign/library/Zend/View/Abstract.php 第 905 行给出

More Information关于我的问题:-

(1) mysql qyery

select  b.tagCode parent,a.tagCode child from tag a, tag b where a.tagParentId=b.tagId

(2) Zend_Debug::dump($this->Tagkey) 的输出;

array(9) {
  [0] => array(2) {
    ["parent"] => string(5) "SK336"
    ["child"] => string(2) "CP"
  }
  [1] => array(2) {
    ["parent"] => string(5) "SK336"
    ["child"] => string(3) "PES"
  }
  [2] => array(2) {
    ["parent"] => string(5) "SK336"
    ["child"] => string(3) "JCP"
  }
  [3] => array(2) {
    ["parent"] => string(5) "SK996"
    ["child"] => string(2) "KO"
  }
  [4] => array(2) {
    ["parent"] => string(5) "SK996"
    ["child"] => string(3) "RTY"
  }
  [5] => array(2) {
    ["parent"] => string(5) "SK996"
    ["child"] => string(3) "HGR"
  }
  [6] => array(2) {
    ["parent"] => string(5) "SK547"
    ["child"] => string(3) "GPK"
  }
  [7] => array(2) {
    ["parent"] => string(5) "SK478"
    ["child"] => string(3) "JUP"
  }
  [8] => array(2) {
    ["parent"] => string(5) "SK336"
    ["child"] => string(3) "BGH"
  }
}

【问题讨论】:

  • 您可能需要自己构建它。我在 ZF 没见过这样的东西。

标签: php zend-framework


【解决方案1】:

现在我可以理解你的问题了。我认为很难从子表单中处理这种想法。尝试按以下方式使用 zend 视图脚本。

你的form.php

public function init()
{
    foreach ($parents as $parent) {

        $parent = new Zend_Form_Element_Hidden($parent);
        $parent->setDecorators(array(
                array(
                      'ViewScript',
                       array(
                        'viewScript' => 'customviewscripts/parent.phtml',
                        'parent' => $parent           
                    )
                )
            );
       $this->addElement($parent);     
    }

}

views/script/customviewscript/parent.phtml 中的文件

<?php

    $params = $this->element->getDecorator('ViewScript')->getOptions();
    $parent = $parems['parent'];

    $string = '<label>$parent['name']</label><input type="check" name="parent[]">';
    foreach ($children as $child) {
         $string .= <label>$child['name']</label>
                 . <input type="check" name=child[$parent[id]][]>' ;

    }

    print $string; 
?>

这不是真正的解决方案。我只是一个例子。我认为您可以自定义它。大多数开发人员使用视图脚本来制作复杂的表单。

【讨论】:

    【解决方案2】:

    你看起来像是提到了多选项的错误语法约翰,你应该试试这个。

    删除 $parent 的数组见下面的例子。

     new Zend_Form_Element_MultiCheckbox('parent', array(
                    'multiOptions' =>  $parent,
                    'filters'      => array('StringTrim'),
                    'validators'   => array(
                    array('InArray',
                          false,
                          array($parent))
                        )
    

    从 db 结果中,您必须为 $parent 变量生成以下类型的数组

    $parent 变量应该像这样,例如复制这个数组并尝试你自己而不从数据库中获取,你会看到 所有选项

    $parent=Array ([1] => blah1 [2] => blah2 [3] => blah3 [4] => blah4 [5] => blah5);
    

    检查这个也有多个复选框,代替数组你应该尝试放置数组变量,我没有尝试过这个只是看着互联网但应该可以正常工作。

    $category1 = new Zend_Form_Element_MultiCheckbox('categories',Array())
    $category1->setLabel('Category 1');
    
    $category2 = new Zend_Form_Element_MultiCheckbox('categories',Array())
    $category2->setLabel('Category 2');
    

    ...稍后...

     $this->addElement($category1)
     ->addElement($category2);
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多