以数组表示法渲染zend子表单元素

时间:2014-06-02 10:18:56

标签: zend-framework zend-form

您好我正在使用以下代码在zend中创建一个子表单:

class Admin_Form_StudentAdmission extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $personalDetailsForm = new Zend_Form_SubForm();
        $personalDetailsForm->setIsArray(true);
        $student_first_name = $this->CreateElement('text','first_name')
                                ->setAttribs(array('placeholder'=>'First Name', 'mendatory'=>'true'))
                                ->setRequired(true)
                                ->addValidators(array(
                                        array('NotEmpty', true, array('messages' => 'Please enter First Name')),
                                        array('stringLength',true,array(2, 10, 'messages'=> 'First Name should be 2 to 10 characters long.')),
                                ))
                                ->addFilter(new Zend_Filter_StringTrim())
                                ->setDecorators(array( array('ViewHelper') 
        ));

        $personalDetailsForm->addElement($student_first_name);
        $this->addSubForm($personalDetailsForm, 'student_personal_details');
    }
}

现在我用下面的PHP代码渲染这个表单:

$personalDetailsForm = $this->form->getSubForm('student_personal_details');
echo $personalDetailsForm->first_name;

但是这会将元素呈现为

<input type="text" mendatory="true" placeholder="First Name" value="" id="first_name" name="first_name">

虽然我希望如下所示

<input type="text" mendatory="true" placeholder="First Name" value="" id="student_personal_details-first_name" name="student_personal_details[first_name]">

我在这里做错了什么?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

只需使用zend MVC模式,就像魅力一样, 结果:

<input type="text" name="student_personal_details[first_name]" id="student_personal_details-first_name" value="" placeholder="First Name" mendatory="true">

将表单作为$ personalDetailsForm从namecontroller.php action viewfromAction()传递给viewfrom.phtml,如:

$this->view->form = $personalDetailsForm;

然后在viewfrom.phtml中回复你的内容,就像:

<?php echo $this->form ?>

Zend 1.12 MVC: [http://framework.zend.com/manual/1.12/en/learning.quickstart.intro.html]

问题看来,你使用了直接echo $ personalDetailsForm-&gt; first_name;那些没有生成正确的html表单元素标签。