解析在zend Farmework下如何創(chuàng)立一個(gè)FORM表單
更新時(shí)間:2013年06月28日 15:50:06 作者:
本篇文章是對(duì)在zend Farmework下如何創(chuàng)立一個(gè)FORM表單的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
1.首先讓我們?cè)O(shè)置一下我們的程序,讓Zend能夠自動(dòng)載入方法,不需要我們手動(dòng)的去載入
require_once 'Zend/Loader/Autoloader.php' //載入自動(dòng)加載類
$loader = Zend_Loader_Autoloader::getInstance();//自動(dòng)實(shí)例化
$loader->registerNamespace('Application_');//注冊(cè)命名空間(只有系統(tǒng)默認(rèn)的,和注冊(cè)的才可以被自動(dòng)載入)
$loader->registerNamespace(array('Foo_', 'Bar_')); //多個(gè)命名空間的注冊(cè)方法
$loader->setFallbackAutoloader(true); //一個(gè)增加消耗的方法,不需要命名空間,直接載入所有類(不被推薦使用)
然后請(qǐng)注意,你的包含目錄是否已經(jīng)包含了,你自己的需被載入的目錄
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/forms/'),
get_include_path(),
)));
//這里我們包含了我們的forms目錄,方便程序的被載入
2.確認(rèn)下form的目錄
在application/forms/下 建立一個(gè) 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');//設(shè)置提交方式
// 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,
));
}
}
然后添加一個(gè)路由控制文件
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"); 手動(dòng)加載類,只有不能自動(dòng)載入時(shí),才需要
$form = new Application_Form_Guestbook;//實(shí)例化這個(gè)方法
if ($this->getRequest()->isPost()) {//如果是POST傳遞的結(jié)果
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;//將表單賦值給試圖
}
}
最后添加一個(gè)簡(jiǎn)單的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;
復(fù)制代碼 代碼如下:
require_once 'Zend/Loader/Autoloader.php' //載入自動(dòng)加載類
$loader = Zend_Loader_Autoloader::getInstance();//自動(dòng)實(shí)例化
$loader->registerNamespace('Application_');//注冊(cè)命名空間(只有系統(tǒng)默認(rèn)的,和注冊(cè)的才可以被自動(dòng)載入)
$loader->registerNamespace(array('Foo_', 'Bar_')); //多個(gè)命名空間的注冊(cè)方法
$loader->setFallbackAutoloader(true); //一個(gè)增加消耗的方法,不需要命名空間,直接載入所有類(不被推薦使用)
然后請(qǐng)注意,你的包含目錄是否已經(jīng)包含了,你自己的需被載入的目錄
復(fù)制代碼 代碼如下:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/forms/'),
get_include_path(),
)));
//這里我們包含了我們的forms目錄,方便程序的被載入
2.確認(rèn)下form的目錄
在application/forms/下 建立一個(gè) Guestbook.phps
作為我們form的類文件,如下:
復(fù)制代碼 代碼如下:
<?php
class Application_Form_Guestbook extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');//設(shè)置提交方式
// 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,
));
}
}
然后添加一個(gè)路由控制文件
applictaion/controller/GuestbookController.php
復(fù)制代碼 代碼如下:
<?php
class GuestbookController extends Zend_Controller_Action
{
// snipping indexAction()...
public function signAction()
{
$request = $this->getRequest();//獲取接受到得信息
// include_once("../application/forms/Guestbook.php"); 手動(dòng)加載類,只有不能自動(dòng)載入時(shí),才需要
$form = new Application_Form_Guestbook;//實(shí)例化這個(gè)方法
if ($this->getRequest()->isPost()) {//如果是POST傳遞的結(jié)果
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;//將表單賦值給試圖
}
}
最后添加一個(gè)簡(jiǎn)單的sign視圖文件即可:
地址:application/views/scripts/guestbook/sgin.php
復(fù)制代碼 代碼如下:
Please use the form below to sign our guestbook!
<?php
$this->form->setAction($this->url());
echo $this->form;
您可能感興趣的文章:
- Zend 輸出產(chǎn)生XML解析錯(cuò)誤
- Zend Studio for Eclipse的java.lang.NullPointerException錯(cuò)誤的解決方法
- apache提示Failed loading ZendLoader.dll解決方法
- 完美解決令人抓狂的zend studio 7代碼提示(content Assist)速度慢的問題
- 使用zend studio for eclipse不能激活代碼提示功能的解決辦法
- Zend Framework實(shí)現(xiàn)多文件上傳功能實(shí)例
- Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例(附demo源碼下載)
- Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法(附demo源碼下載)
- Zend Framework框架教程之Zend_Db_Table_Rowset用法實(shí)例分析
- Zend Framework教程之Zend_Db_Table_Row用法實(shí)例分析
- Zend Framework教程之Zend_Db_Table用法詳解
- Zend Framework入門知識(shí)點(diǎn)小結(jié)
- Zend Framework基本頁面布局分析
- Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法
相關(guān)文章
解析PHP中的file_get_contents獲取遠(yuǎn)程頁面亂碼的問題
本篇文章是對(duì)PHP中的file_get_contents獲取遠(yuǎn)程頁面出現(xiàn)亂碼的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06解析phpstorm + xdebug 遠(yuǎn)程斷點(diǎn)調(diào)試
本篇文章是對(duì)phpstorm + xdebug 遠(yuǎn)程斷點(diǎn)調(diào)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php獲取網(wǎng)頁里所有圖片并存入數(shù)組的方法
這篇文章主要介紹了php獲取網(wǎng)頁里所有圖片并存入數(shù)組的方法,涉及php正則匹配及數(shù)組操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04簡(jiǎn)單采集了yahoo的一些數(shù)據(jù)
簡(jiǎn)單采集了yahoo的一些數(shù)據(jù)...2007-02-02詳解php中implode explode serialize json msgpack性能對(duì)比
這篇文章主要介紹了php中implode/explode、serialize、json、 msgpack性能對(duì)比,對(duì)性能感興趣的同學(xué),可以參考下2021-04-04PHP簡(jiǎn)單實(shí)現(xiàn)無限級(jí)分類的方法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)無限級(jí)分類的方法,涉及sql語句及遞歸調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-05-05PHP實(shí)現(xiàn)批量清空刪除指定文件夾所有內(nèi)容的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)批量清空刪除指定文件夾所有內(nèi)容的方法,涉及php基于自定義函數(shù)遞歸調(diào)用實(shí)現(xiàn)刪除指定目錄下文件與文件夾相關(guān)操作技巧,需要的朋友可以參考下2017-05-05