2
votes

Hi I am making a sub form in zend with the following code:

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');
    }
}

And now I am rendering this form with below php code:

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

But this renders the element as

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

While I want this as below

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

What I'm doing wrong here?? could anyone help please??

1

1 Answers

0
votes

Just use zend MVC pattern, it will work like a charm, result:

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

Pass the the form as $personalDetailsForm from namecontroller.php action viewfromAction() to viewfrom.phtml like:

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

Then echo your from in the viewfrom.phtml simply like:

<?php echo $this->form ?>

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

The problem looks like, you used a direct echo $personalDetailsForm->first_name; thats didn't generate the proper html form element tags.