I'm new to Zend Framework. I'm trying to make a simple login form, but I got a problem that I don't know the solution. The problem is that the form elements are not showing. Instead, the form tag is created (I can see it through the page source code). Why can the form elements are not showing while the form tag is created. Here are the codes.
File application/forms/Account.php
class Application_Form_Account extends Zend_Form
{
public function开发者_如何学编程 init()
{
// Initiate form
$this->setAction('account/login-exec');
$this->setMethod('post');
$this->setName('login-form');
// Add form elemen: username
$username = new Zend_Form_Element_Text('username');
$username -> setLabel('User Name');
// Add form element: Password
$password = new Zend_Form_Element_Password('password');
$password -> setLabel('Password');
// Add form elemet: Submit button
$submitButton = new Zend_Form_Element_Submit('submit');
$submitButton -> setLabel('Post Data');
}
}
File application/controllers/AccountControllers.php
class AccountController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
// Show the login form in view
$form = new Application_Form_Account();
$this->view->pageHeading = "Please Login!!";
$this->view->form = $form;
}
}
File application/views/index.phtml
<h1><?php echo $this->pageHeading ?></h1>
<?php echo $this->form; ?>
In the view, the pageHeading and the form tag are showing, but none the elements are.
Thank you for your help
You aren't adding any of your elements to the form. In the form's init()
method, use addElement()
, eg
$this->addElement($username);
See http://framework.zend.com/manual/en/zend.form.quickstart.html#zend.form.quickstart.elements
You are actually creating the form element but not fixing/attaching it to form.you can assume this as three step process
- create Zend Form element like
$selector = new Zend_Form_Element_Select('reseller_id');
- Add attributes,validators or filters like
$selector->setLabel('Reseller')
->setRequired(false)
->setMultiOptions($pairs);
- Append this element to Form
$this->addElement($selector);
Complete code :
$selector = new Zend_Form_Element_Select('reseller_id');
$selector->setLabel('Reseller')
->setRequired(false)
->setMultiOptions($pairs);
$this->addElement($selector);
精彩评论