찾다
php教程php手册解析在zend Farmework下如何创立一个FORM表单

本篇文章是对在zend Farmework下如何创立一个FORM表单的方法进行了详细的分析介绍,需要的朋友参考下

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的类文件,如下:

复制代码 代码如下:


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

复制代码 代码如下:


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!
$this->form->setAction($this->url());
echo $this->form;


성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구