intro
In this article, Zend Form component is used reffering to Official ZF QuickStart.You can download the source code of this article from here.
1.Create Controller
Create the file
application/constorllers/IndexController.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php /** @see Zend_Controller_Action */ require_once 'Zend/Controller/Action.php'; class IndexController extends Zend_Controller_Action { /** * This function returns the form used for * adding comments in blog application */ public static function getAddCommentForm() { require_once 'Zend/Form.php'; $form = new Zend_Form(array( 'method' => 'post', 'elements' => array( 'comments' => array('textarea', array( 'required' => true, 'label' => 'Write your comments' )), 'submit' => array('submit', array( 'label' => 'Send' )) ), )); return $form; } public function indexAction() { $form = $this->getAddCommentForm(); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = $form->getValues(); $this->view->values = $values; } } $this->view->form = $form; } } |
2.Create View
Create the file
application/views/scripts/index/index.phtml
as following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Zend Framework Quick Start</title> </head> <body> <h1 id="5_basic-blog_1" >Basic Blog</h1> <?php if($this->values) : ?> <h3 id="5_you-just-submitted-t_1" >You just submitted the following values: </h3> <ul> <?php foreach ($this->values as $value) :?> <li> <?= $this->escape($value); ?> </li> <?php endforeach; ?> </ul> <?php endif; ?> <?= $this->form; ?> </body> </html> |
3.Check
Let’s access your web server(ex.http://localhost/).
Next
I will study more about Zend_Form.
Nice. Just a small word though… you shouldn’t access a static method as within the object ( self:: instead of $this )
That’s right:)
Thank you for your correct comment!