解析在zend Farmework下如何创立一个FORM表单

2015-01-24信息快讯网

本篇文章是对在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的类文件,如下:
<?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;

php导出word文档与excel电子表格的简单示例代码
详解PHP中strlen和mb_strlen函数的区别
Drupal7 form表单二次开发要点与实例
分享下页面关键字抓取components.arrow.com站点代码
PHP $_FILES中error返回值详解
使用openssl实现rsa非对称加密算法示例
php fsockopen解决办法 php实现多线程
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
浅析ThinkPHP中的pathinfo模式和URL重写
php二维数组排序方法(array_multisort usort)
zend framework文件上传功能实例代码
php foreach循环中使用引用的问题
FireFox浏览器使用Javascript上传大文件
php读取本地文件常用函数(fopen与file_get_contents)
完美解决PHP中的Cannot modify header information 问题
php number_format() 函数通过千位分组来格式化数字的实现代码
解析php防止form重复提交的方法
zend Framework中的Layout(模块化得布局)详解
解析zend Framework如何自动加载类
解析file_get_contents模仿浏览器头(user_agent)获取数据
如何在smarty中增加类似foreach的功能自动加载数据
深入file_get_contents与curl函数的详解
执行、获取远程代码返回:file_get_contents 超时处理的问题详解
解析PHP中的file_get_contents获取远程页面乱码的问题
探讨:如何使用PhpDocumentor生成文档
探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释
PHP中的函数-- foreach()的用法详解
解析coreseek for sphinx的使用
探讨GDFONTPATH能否被winxp下的php支持
解析phpstorm + xdebug 远程断点调试
深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
php中unlink()、mkdir()、rmdir()等方法的使用介绍
©2014-2024 dbsqp.com