Reputation: 15108
I am using zend framework and am trying to output a simple login form using zend form, MVC and OOP.
My code is below: The Controller IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->loginForm = $this->getLoginForm();
}
public function getLoginForm()
{
$form = new Application_Form_Login;
return $form;
}
}
This is the form: Login.php
class Application_Form_Login extends Zend_Form
{
public function init()
{
$form = new Zend_Form;
$username = new Zend_Form_Element_Text('username');
$username
->setLabel('Username')
->setRequired(true)
;
$password = new Zend_Form_Element_Password('password');
$password
->setLabel('Password')
->setRequired(true)
;
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Login');
$form->addElements(array($username, $password, $submit));
}
}
And the view: index.phtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<div id="header">
<div id="logo">
<img src="../application/images/logo.png" alt="logo">
</div>
</div>
<div id="wrapper">
<?php echo $this->loginForm; ?>
</div>
</body>
</html>
I am new to Zend Framework, MVC and OOP, So this my best attempt at this following online advice, tutorials etc.
Upvotes: 0
Views: 107
Reputation: 33148
You have inadvertently created a form with no elements, which is why nothing is appearing. In the init method of your form object you are creating a new instance of Zend_Form
, $form
which you then do nothing with, instead of adding the elements to the current instance. Change your class to this:
class Application_Form_Login extends Zend_Form
{
public function init()
{
$username = new Zend_Form_Element_Text('username');
$username
->setLabel('Username')
->setRequired(true)
;
$password = new Zend_Form_Element_Password('password');
$password
->setLabel('Password')
->setRequired(true)
;
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Login');
$this->addElements(array($username, $password, $submit));
}
}
and it should work.
Upvotes: 5
Reputation: 10206
Try doing something like this instead:
http://framework.zend.com/manual/en/zend.form.forms.html
Upvotes: 1