1

I am following this thread to create a form and it is working when No. of form elements are known. Now I have to create form fields based on user choice.

For Example:

<?php class Form_MyForm extends Zend_Form { public function __construct( $options = null ) { parent::__construct( $options ); // Set the method for the display form to POST $this->setMethod('post'); $elements = array(); // Get user input to create elements $fields = $options['columns']; // Create form elements for( $i = 0; $i < count( $fields ); $i++ ) { $element = $this->CreateElement('text', 'field'.$i ); $element->setLabel( $fields[$i]['name'] ); $elements[] = $element; } $this->addElements( $elements ); $this->setElementDecorators( array( 'ViewHelper' ) ); $this->setDecorators( array( array( 'ViewScript', array( 'viewScript' => 'myform-form.phtml' ) ) ) ); } // end construct } // end class ?> 

I can render each element separately but Now I don't know how to render these elements in myform-form.phtml by looping. I have to loop because No. Fields are not known at the begining..

Thanks

3
  • I'm not exactly a Zend_Form wiz (I actually hate it), but maybe you should move your code into another method (say configureForm() and have it accept an argument with the number of fields instead? Commented Jun 11, 2011 at 20:34
  • What is your $columns variable? I don't see where it is created. Commented Jun 12, 2011 at 1:39
  • @Marcin: Sorry it was typo. It is actually $field array. It contains Labels of form fields. For Example: array('FirstName','LastName','BirthDate'); Please review the question again. Thanks Commented Jun 12, 2011 at 4:46

2 Answers 2

1

should use something like this (didn't test it)

<? foreach ($this->element->getElements() as $element){ echo $this->{$element->helper}( $element->getName(), 1, $element->getAttribs() ) } ?> 

$this->element should be your form

Sign up to request clarification or add additional context in comments.

Comments

0

Side note to start: It's typical to do all this in the init() method rather than overriding the form constructor. But that's not really a big deal.

Re: rendering of the fields using the view script of ViewScript decorator: In myform-form.phtml, seems like you could call $this->getOption('columns') and then perform a foreach loop to render the elements, analogous to the loop you used to create the fields.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.