John
John

Reputation: 329

zend form Issue in zend framework

I am using zend Framework Form, I am newbie in zend Framework and i want to display my check box form like this :-

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

Note where :- * is check-box here

what i am trying is here :-

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,
        )
    );
    }

I am able to create such type of structure in any .php page but not able to do that right now in zend framework form, I am using zend sub-form here.

Also I got an error right now when i am using this query

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /var/www/dashboard_campaign/library/Zend/View/Abstract.php on line 905

More Information about my Question :-

(1) mysql qyery

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

(2) output of 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"
  }
}

Upvotes: 0

Views: 845

Answers (2)

Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

Now i can understand your problem. I think it is to hard for handle that think from sub form. Try to use zend view scripts as following way.

Your 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);     
    }

}

file at 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; 
?>

This is not the real solution. I percent only example. I think you can customize that. Most of developer use view script to make complex forms.

Upvotes: 1

Guru
Guru

Reputation: 419

Your looking like mentioned wrong syntax for multi options John, you should try this.

Remove array for $parent see example below.

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

From db results you have to produce following type of array for the $parent variable

$parent variable should like this for example copy this array and try your self without fetching from database,you will see all options

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

Check this one also for multiple check boxes, in place of array you should try placing array variable, I havn't tried this just looked at internet but should work fine.

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

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

... later...

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

Upvotes: 0

Related Questions