在zend Farmework下怎么创建一个FORM表单
创始人
2024-12-14 20:02:34
0次

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;

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...