ZF Create Form


 

ZF Create Form

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

0

}

else

1

{

$form->populate($formData);

}

2

}

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

3

}

Again open the command prompt, change the directory to the project home directory and write down the following code:

4

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

5

public function loginAction()

{

$request = $this->getRequest();

6

$this->view->title="Login Section";

$this->view->headTitle($this->view->title);

7

$form = new Application_Form_Guestbook();

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

8

if ($this->getRequest()->isPost()) {

9

if ($form->isValid($request->getPost())) {

$comment = new Application_Model_Guestbook($form->getValues());

0

$mapper = new Application_Model_GuestbookMapper();

1

$mapper->save($comment);

return $this->_helper->redirector('index');

2

}

else

3

{

$form->populate($formData);

}

4

}

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

}

5

At last open the login.phtml file and change the existing content with the following code:

<?php

6

$this->form->setAction($this->url());

echo $this->form;

7

 

Ads