English 中文(简体)
dynamicaly adding textboxes to zend_form
原标题:

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...

最佳回答

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
   }
}
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签