Zend Framework, Form - Form renders with just one field
I am using ZF Form to generate forms. I wrote a class
class Form_Client extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$name = $this->createElement('text', 'email',array('label'=>'Name','size'=>'50'));
$name->setErrorMessages(array('Field is required'));
$name->setRequired(TRUE);
$this->addElement($name);
$contact_person = $this->createElement('text', 'email',array('label'=>'Contact person','size'=>'50'));
$contact_person->setErrorMessages(array('Field is required'));
$contact_person->setRequired(TRUE);
$this->addElement($contact_person);
// add element: submit button
$submit = $this->createElement('submit', 'submit', array('label' => 'Save'));
//$submit->setDecorators(array('ViewHelper'));
$this->addElement($submit);
$btn = $this->createElement('button', 'cancel', array('label' => 'Cancel'));
$btn->setAttribs(array('onClick'=>'window.location="/system/login"','style'=>''));
//$btn->setDecorators(array('ViewHelper'));
$this->addElement($btn);
}
}
In my controller I've got
public function editAction()
{
$frmClient = new Form_Client();
$frmClient->setA开发者_JAVA百科ction('edit');
/*submit*/
if ($this->_request->isPost()) {
if ($frmClient->isValid($_POST)) {
$data = $frmClient->getValues();
}
}
$this->view->form = $frmClient;
$this->_helper->viewRenderer('form');
}
And in my View there's
<?php echo $this->form; ?>
The problem is that form renders with just the first input fields. Do you know what's wrong with that?
Thanks, Jacob
Both of your text fields are called "email" - Each field must have a unique name.
Currently, both form elements have the same name. Try something like:
[..]
$name = $this->createElement('text', 'name',array('label'=>'Name','size'=>'50'));
[..]
$contact_person = $this->createElement('text', 'contact_person',array('label'=>'Contact person','size'=>'50'));
[..]
精彩评论