解析在zend Farmework下如何創立一個FORM表單

來源:互聯網
上載者:User

1.首先讓我們設定一下我們的程式,讓Zend能夠自動載入方法,不需要我們手動的去載入
複製代碼 代碼如下:require_once 'Zend/Loader/Autoloader.php'    //載入自動載入類
$loader = Zend_Loader_Autoloader::getInstance();//自動執行個體化
$loader->registerNamespace('Application_');//註冊命名空間(只有系統預設的,和註冊的才可以被自動載入)
$loader->registerNamespace(array('Foo_', 'Bar_')); //多個命名空間的註冊方法
$loader->setFallbackAutoloader(true);  //一個增加消耗的方法,不需要命名空間,直接載入所有類(不被推薦使用)

然後請注意,你的包含目錄是否已經包含了,你自己的需被載入的目錄複製代碼 代碼如下:set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/forms/'),
    get_include_path(),
)));
//這裡我們包含了我們的forms目錄,方便程式的被載入

2.確認下form的目錄
在application/forms/下 建立一個  Guestbook.phps
作為我們form的類檔案,如下:複製代碼 代碼如下:<?php
 class Application_Form_Guestbook extends Zend_Form
{
    public function init()
    {
        // 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,
        ));
    }
}

然後添加一個路由控制檔案
applictaion/controller/GuestbookController.php複製代碼 代碼如下:<?php
class GuestbookController extends Zend_Controller_Action
{
    // snipping indexAction()...
    public function signAction()
    {
        $request = $this->getRequest();//擷取接受到得資訊
       // include_once("../application/forms/Guestbook.php");  手動載入類,只有不能自動載入時,才需要
        $form    = new Application_Form_Guestbook;//執行個體化這個方法

        if ($this->getRequest()->isPost()) {//如果是POST傳遞的結果
            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;//將表單賦值給試圖
    }
}

最後添加一個簡單的sign視圖檔案即可:
地址:application/views/scripts/guestbook/sgin.php複製代碼 代碼如下:Please use the form below to sign our guestbook!
<?php
$this->form->setAction($this->url());
echo $this->form;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.