In this tutorial we will learn how to create forms in Zend Framwork. In this current tutorial we are using ZF 1.10 version and in this version most of the task are done from the command line interfaces. The details are discussed in this tutorial.
In this tutorial we will learn how to create forms in Zend Framwork. In this current tutorial we are using ZF 1.10 version and in this version most of the task are done from the command line interfaces. The details are discussed in this tutorial.Login Form in ZF:
It is most common activity in any web application that we need to send data from one page to another through forms, in ZF we can create forms and put some text box, password type text box and other UI and at the same time we can also put some validation checking through ZF.
Probably it would be the best option to create a Login form to know the details about form and other actions.
Go through the following code and you will come to know how to create forms and what will be the exact coding to display a form and its contents:
In ZF we do not create and display the forms in usual way (like we do in simple html files), in ZF we need to write commands in command prompt:
zf create form login
where login is the name of the form , and this command will create one file one is ../application/forms/Login.php and
it will also update the project profile /.zfproject.xml
Now replace the existing coding of Login.php with the following code, :
<?php
public function loginAction()
{
$request = $this->getRequest();
$this->view->title="Login Section";
$this->view->headTitle($this->view->title);
$form = new Application_Form_Guestbook();
$this->view->form=$form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$comment = new Application_Model_Guestbook($form->getValues());
$mapper = new Application_Model_GuestbookMapper();
$mapper->save($comment);
return $this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
$this->view->form = $form;
}
Again open the command prompt, change the directory to the project home directory and write down the following code:
zf create action login index
The above command will create an action method called login in indexController. After that open the IndexController.php page and paste the following code in the loginAction() method:
<?php
public function loginAction()
{
$request = $this->getRequest();
$this->view->title="Login Section";
$this->view->headTitle($this->view->title);
$form = new Application_Form_Guestbook();
$this->view->form=$form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$comment = new Application_Model_Guestbook($form->getValues());
$mapper = new Application_Model_GuestbookMapper();
$mapper->save($comment);
return $this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
$this->view->form = $form;
}
At last open the login.phtml file and change the existing content with the following code:
<?php
$this->form->setAction($this->url());
echo $this->form;