首頁 > PHP, Zend Framework > Zend Framework 產生 FORM

Zend Framework 產生 FORM

2011年1月21日 發表評論 閱讀評論

產生留言版的 FORM

指令:
zf create form Guestbook

andy@nb3vm:~/www/rdlab/application$ zf create form Guestbook
Creating a form at /home/andy/www/rdlab/application/forms/Guestbook.php
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’

產生 application/forms/Guestbook.php

編輯一下原本的內容修改成我們需要的,如下:

<?php
class Application_Form_Guestbook extends Zend_Form
{

    public function init()
    {
        /* Form Elements & Other Definitions Here ... */
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add an email element
        $this->addElement('text', 'email', array(
            'label'      => 'Your email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
            )
        ));

        // Add the comment element
        $this->addElement('textarea', 'comment', array(
            'label'      => 'Please Comment:',
            'required'   => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(0, 20))
                )
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'label'      => 'Please enter the 5 letters displayed below:',
            'required'   => true,
            'captcha'    => array(
                'captcha' => 'Figlet',
                'wordLen' => 5,
                'timeout' => 300
            )
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Sign Guestbook',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}

接著要將 signAction() 加到 GuestbookController 以處理 FORM 的遞送
用以下的方式建立 action並且關聯到 view 裡

andy@nb3vm:~/www/rdlab/application$ zf create action sign Guestbook
Creating an action named sign inside controller at /home/andy/www/rdlab/application/controllers/GuestbookController.php
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’
Creating a view script for the sign action method at /home/andy/www/rdlab/application/views/scripts/guestbook/sign.phtml
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’

經由執行結果看到新增了一個 signAction() 方法到 controller 以及相對應的 view 內容。


接著來修正一下 controller 的內容(只顯示 signAction()):

class GuestbookController extends Zend_Controller_Action
{
    public function signAction()
    {
        $request = $this->getRequest();
        $form    = new Application_Form_Guestbook();

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

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

當然,再次的修改 view 以顯示內容
檔案:application/views/scripts/guestbook/sign.phtml

<br /><br /><center>View script for controller <b>Guestbook</b> and script/action name <b>sign</b></center>

Please use the form below to sign our guestbook!

<?php
$this->form->setAction($this->url());
echo $this->form;

這樣就有了基本的 Zend Framework Form 了

Categories: PHP, Zend Framework Tags: ,
  1. 目前尚無任何的評論。