Home > Zend_Form > Use of Zend Form

Use of Zend Form

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.

<?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.

<!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.

Comments:2

Jonathan 08-09-11 (Thu) 4:41

Nice. Just a small word though… you shouldn’t access a static method as within the object ( self:: instead of $this )

oplabo 08-09-15 (Mon) 20:24

That’s right:)
Thank you for your correct comment!

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/5/trackback
Listed below are links to weblogs that reference
Use of Zend Form from Open Programming Laboratory

Home > Zend_Form > Use of Zend Form

Japanese
Search
Feeds

Return to page top