Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在zend中创建表单_Php_Zend Framework_Forms - Fatal编程技术网

Php 如何在zend中创建表单

Php 如何在zend中创建表单,php,zend-framework,forms,Php,Zend Framework,Forms,我按照线程创建了一个表单,当表单元素的数量已知时,它就开始工作了。现在,我必须根据用户选择创建表单字段 例如: <?php class Form_MyForm extends Zend_Form { public function __construct( $options = null ) { parent::__construct( $options ); // Set the method for the display form to

我按照线程创建了一个表单,当表单元素的数量已知时,它就开始工作了。现在,我必须根据用户选择创建表单字段

例如:

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

但是现在我不知道如何通过循环在myform-form.phtml中呈现这些元素。我必须循环,因为没有。字段在开始时是未知的


首先感谢旁注:通常在
init()
方法中执行所有这些操作,而不是重写表单构造函数。但这其实没什么大不了的


Re:使用
ViewScript
decorator:In
myform form.phtml
的视图脚本呈现字段,似乎您可以调用
$this->getOption('columns')
,然后执行
foreach
循环来呈现元素,类似于您用来创建字段的循环。

开始注意:通常在
init()
方法中执行所有这些操作,而不是重写表单构造函数。但这其实没什么大不了的


Re:使用
ViewScript
decorator:In
myform form.phtml
的视图脚本呈现字段,似乎您可以调用
$this->getOption('columns')
,然后执行
foreach
循环来呈现元素,类似于您用来创建字段的循环。

应该使用类似的内容 (没有测试)



$this->element
应该是您的表单

应该使用类似的内容 (没有测试)



$this->element
应该是你的表单

我不是Zend_表单高手(我真的很讨厌它),但也许你应该将代码转移到另一个方法中(比如
configureForm()
并让它接受带有字段数的参数?您的
$columns
变量是什么?我看不出它是在哪里创建的。@Marcin:很抱歉,它是打字错误。它实际上是$field数组。它包含表单字段的标签。例如:
数组('FirstName','LastName','BirthDate'))
请再复习一下这个问题。谢谢我不是一个真正的Zend_Form wiz(我真的很讨厌它),但也许你应该把你的代码换成另一种方法(比如
configureForm()
并让它接受带有字段数的参数?您的
$columns
变量是什么?我看不出它是在哪里创建的。@Marcin:很抱歉,它是打字错误。它实际上是$field数组。它包含表单字段的标签。例如:
数组('FirstName','LastName','BirthDate'))
请重新检查问题。谢谢
<?
foreach ($this->element->getElements() as $element){
    echo $this->{$element->helper}(
        $element->getName(), 1, $element->getAttribs()
    )
}
?>