0

I have the following php script now i want to put his script in zend forms Here is my code so far :-

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

foreach ($parents as $parent) {
     echo '<div>';
     $parent_value = "'$parent'";
     echo '<div><input type="checkbox" name="parents[]" value="'.$parent.'" id="'.$parent.'" class="parentCheck"/>
     <label for="parents_'.$parent.'">'.$parent.'</label></div>';
         foreach ($childs[$parent] as $child) {
           $child_value = "'$child'";
               echo '<div style="margin-left:15px;"><input type="checkbox" name="childs[]" value="'.$child.'" id="childs_'.$child.'" class="child_'.$parent.'" onclick="checkParent('.$parent_value.','.$child_value.');"/>
              <label for="childs_'.$child.'">'.$child.'</label></div>';
     }
     echo '</div>';
} 

now i am going to use this pure php script in zend form what i am trying is here :-

class Admin_Form_Users extends Zend_Form {

public function init()
{   

   $parents = array();
      $childs = array();

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

       foreach ($parents as $parent) {

       $subForm = new Zend_SubForm();
       $subForm->addElement($parent);

       foreach ($children as $child) {
           $subForm->addElement($child);
       }

       $form->addSubForm($subForm);
    }      


        $parent = new Zend_Form_SubForm();
        $parent->addElements(array(
            new Zend_Form_Element_MultiCheckbox('subscriptions', array(
                'label'        =>
                    'Which parent would you like to subscribe to?',
                'multiOptions' => $parents,
                'required'     => true,
                'filters'      => array('StringTrim'),
                'validators'   => array(
                    array('InArray',
                          false,
                          array(array_keys($parents)))
                )
            )),
        ));

        $child = new Zend_Form_SubForm();
        $child->addElements(array(
            new Zend_Form_Element_MultiCheckbox('subscriptions', array(
                'label'        =>
                    'Which child would you like to subscribe to?',
                'multiOptions' => $childs,
                'required'     => true,
                'filters'      => array('StringTrim'),
                'validators'   => array(
                    array('InArray',
                          false,
                          array(array_keys($childs)))
                )
            )),
        ));


         $this->addSubForms(array(

        '$child' => $child,
        'parent' => $parent
    ));

I got an error

Warning: Invalid argument supplied for foreach() in /var/www/dashboard_campaign/application/modules/admin/forms/Users.php on line 19

means here :- foreach ($this->Tagkey as $aResultDataValue) {

Tagkey comes from database model

what i can do an I am newbie in zend framework what i am doing wrong help me

3
  • What is the output of Zend_Debug::dump($this->Tagkey); Commented Jan 31, 2012 at 5:24
  • @emaillenin :) when i put Zend_Debug::dump($this->Tagkey); in my form i got NULL Commented Jan 31, 2012 at 5:33
  • @emaillenin :) when i put my above pure php script in phtml it works very fine and also got output for Zend_Debug::dump($this->Tagkey); but i have the requirement to work in zend form only Commented Jan 31, 2012 at 5:34

2 Answers 2

1

$TagKey is not a member of Zend_Form, and I don't see where it is being added to it.

You could either pass the model that TagKey comes from into the form's constructor, or in the form's init() method, you need to create an instance of the model and get the TagKey variable.

Here is how you can get it from your controller into the form object.

public function editAction()
{
    $tags = new Campaign_Model_DbTable_Tag();
    $aResultData = $tags->getTagkey();
    $this->view->Tagkey = $aResultData;

    $form = new Admin_Form_Users($aResultData);

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($this->getRequest()->getPost())) {
            // valid
        } else { 
            // errors 
        }
    }
}

Then add a constructor to your form.

// Admin_Form_Model
public function __construct($tagKey)
{   
    $this->tagKey = $tagKey;

    parent::__construct(); // you must call this last as it calls init()
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, I see. Now you somehow need to get it into the Form. Just using it in the controller doesn't do any good for the form object itself.
:) how can i do that please help me
In UserController on line 44 where you create the form object, you have to create it with the tagkey object like $form = new Admin_Form_Users($aResultData);
Then maybe $this->TagKey is not an array or iteratable object, try var_dump to see what it is.
NULL Warning: see my above comments also
0

your problem is simple error:

you set up the variable $childs = array();
then called it as $children:

foreach ($children as $child) {
           $subForm->addElement($child);
       }

this may not be the only error in this script, but it's the one the message deals with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.