views:

48

answers:

1

Hi,

I hope that this is a quick question to answer. I am developing a form using Zend_Form, I have a number of Zend_Dojo_Form_Element_Textboxs to add to this form dynamically.

These are added from rows in the database, e.g.

$count = 0;
   //we now loop through the skill types and add them to the form.
   foreach($skillResult as $skill){

    $skillTextBox = new Zend_Dojo_Form_Element_ValidationTextBox('skill-'.$count,
     array('trim' => true,
      'NotEmpty' => true,
      'invalidMessage' => 'This can not be blank'
     )
    );
    $skillTextBox->addValidator('NotEmpty')
     ->removeDecorator('DtDdWrapper')
     ->removeDecorator('HtmlTag')
     ->removeDecorator('Label');

    //add the element to the form.
    $myForm->addElement($skillTextBox);

    $count++;

   }

The form is then displayed in a view script, that I need to extract however. As I do not know how many 'skill' textboxes exist in the form I am not sure how I can loop through and add them to the view script. I would normally look at adding them to the viewScript in the following way:

<?php foreach($this->element->getElement('skill') as skill) :?>
  <tr>
   <td><?php echo $skill;?></td>
  </tr>
<?php endforeach;?>

However I am getting an error message of Warning: Invalid argument supplied for foreach()

Am I going about this in a backward way and change my approach to this form or am I missing somthing here?

Thanks in advance...

+1  A: 

If you are creating the form in a controller's action function, you can do something like this to tell your view script how many skill text boxes you added..

In controller:

$this->view->skillTextBoxCount = $count;

In view:

// the view is now "this"
$skillCount = $this-skillTextBoxCount;

You could also do something like this:

$elements = $form->getElements();
foreach($elements as $element) {
   if (strpos($element->getName(), 'skill-') === 0) { // must use === here
      // do something with your element
   }
}
Chris Williams