小編給大家分享一下在zend Farmework下怎么創(chuàng)建一個(gè)FORM表單,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.首先讓我們?cè)O(shè)置一下我們的程序,讓Zend能夠自動(dòng)載入方法,不需要我們手動(dòng)的去載入
require_once 'Zend/Loader/Autoloader.php' //載入自動(dòng)加載類(lèi)
$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è)增加消耗的方法,不需要命名空間,直接載入所有類(lèi)(不被推薦使用)
然后請(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的類(lèi)文件,如下:
<?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(//原件的類(lèi)型,名詞,和一些其他信息的定義
'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)加載類(lèi),只有不能自動(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;
以上是“在zend Farmework下怎么創(chuàng)建一個(gè)FORM表單”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:在zendFarmework下怎么創(chuàng)建一個(gè)FORM表單-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://www.chinadenli.net/article0/dcssio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站營(yíng)銷(xiāo)、外貿(mào)建站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容