开发者

How to create a form in zend

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;
       开发者_如何学C }

        $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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜