Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯誤提示的方法,zendzend_form
Jun 13, 2016 am 08:43 AMZend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯誤提示的方法,zendzend_form
本文實(shí)例講述了Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯誤提示的方法。分享給大家供大家參考,具體如下:
同時公司又要開發(fā)一個群組功能..我也就想運(yùn)用一下Zend_Form來實(shí)現(xiàn)創(chuàng)建群組的功能.主要還是看中Zend_Form可以在寫Form時候.實(shí)現(xiàn)服務(wù)器端的驗(yàn)證功能..省得我們在把數(shù)據(jù)提交到數(shù)據(jù)庫的時候再驗(yàn)證一次..所以呢.我就看了一下這方面的手冊..通過Zend Framework手冊找到了相關(guān)的使用說明...最簡單的使用方式就是在控制器(Controller)里寫一個現(xiàn)成的Action,這樣..在這個控制器里就可以直接使用這個Action...代碼可以如下:
<?php public function formAction() { $form=new Zend_Form(); $form->setName('group'); $title = new Zend_Form_Element_Select('title'); $title ->setLabel('性別') ->setMultiOptions(array('mr'=>'Mr', 'mrs'=>'Mrs')) ->setRequired(true) ->addValidator('NotEmpty', true); $yourName = new Zend_Form_Element_Text('firstName'); $yourName->setLabel('姓名') ->setRequired(true) ->addValidator('NotEmpty', true) ; $email = new Zend_Form_Element_Text('email'); $email->setLabel('電子郵件地址') ->addFilter('StringToLower') ->setRequired(false) ->addValidator('NotEmpty'); $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('group'); $form->addElements(array($title, $yourName,$email,$submit)); } ?>
當(dāng)然..我也可以把這個Form專門寫成一個類...存放在一個forms共同的目錄下.這樣就方便我們管理我們所有的Form表單..我的實(shí)現(xiàn)方式就是把它放在和控制器(Controller)的同一級別的目錄下...這樣管理起來也方便..當(dāng)然不同的朋友..有不同的想法...另一種方式..就是可以把它寫成View Helper...這個方式實(shí)現(xiàn)起來,,也很方便..這里我就不想多寫了...Zend Framework實(shí)現(xiàn)起來很方便...只要你想的到...無論你怎樣完成你的任務(wù),,都是可以的..在這里我就不多說其它的...我只想談一下怎樣讓Zend_Form實(shí)現(xiàn)中文的提示信息功能...我這里有二種方法..
第一:比較笨的方式就是:如果你的網(wǎng)站不要做成多國語言的網(wǎng)站..同時你的Zend Framework版本不是經(jīng)常更換的話...你就可以找到相關(guān)提示信息的源碼...更改成中文的提示.
這個笨方法..實(shí)在是沒有辦法的辦法...呵呵...
第二:我也是在英文站...看到的一個比較好的方式,就是通過重寫這個提示信息.把它換成我們想要的語言...這樣...就算我們會去換語言..或是換Zend Framework的版本..
對我們的影響也不是很大...我們只要更改一下我們的Form的表單就可以搞定了..現(xiàn)在這種方式的代碼如下(我這里只寫了Email提示信息..其它的不要多寫出):
<?php $email = new Zend_Form_Element_Text('email'); $email->setLabel('電子郵件地址') ->addFilter('StringToLower') ->setRequired(false) ->addValidator('NotEmpty') ->addValidator('EmailAddress',true,array('messages' => array( 'emailAddressInvalid' => '這不是一個可用的電子郵件!', 'emailAddressInvalidHostname' => '這不是一個有效的主機(jī)名!', 'emailAddressInvalidMxRecord' => '這不是一個有效的電子郵件地址!', 'emailAddressDotAtom' => '這不是一個有效的電子郵件地址!', 'emailAddressQuotedString' => '這不是一個有效的電子郵件地址!', 'emailAddressInvalidLocalPart' => '這不是一個有效的電子郵件地址!', ))); ?>
到這里..Zend_Form這個組件還有一個比較重要的功能..就是Zend_Form_Decorator..手冊上稱為裝飾器,也就是說你可以自己寫你想要的裝飾器..比如說..你要把你的Form用Table包含起來..我們要怎樣實(shí)現(xiàn)呢?這個時候..我們就要用到比如說HtmlTag,Label這些裝飾器來達(dá)到我們想要的功能...這里是一個比較重要的概念了..有興趣的朋友可以去去看一下...因?yàn)槿绻阆胍肸end_Form這個組件..不會裝飾器因該用起來會很困難..所以必須要會這個東西..才可以創(chuàng)建你自己想要的表單功能..最后..就是一點(diǎn)裝飾器的小運(yùn)用
我只是實(shí)現(xiàn)一個小的功能...如下代碼:
<?php $email = new Zend_Form_Element_Text('email'); $email->setLabel('電子郵件地址') ->addFilter('StringToLower') ->setRequired(false) //利用裝飾器來增加td標(biāo)簽 ->addDecorator('HtmlTag', array('tag' => 'td')) ->addDecorator('Label', array('tag' => 'td')) //重復(fù)利用HtmlTag裝飾器來增加tr標(biāo)簽 ->addDecorator(array('FooTr' => 'HtmlTag'), array('tag' => 'tr')) ->addValidator('NotEmpty'); ?>
哈哈....大致的運(yùn)用就是這樣了...最后..就是驗(yàn)證提交的數(shù)據(jù)了...看如何驗(yàn)檢驗(yàn)用戶提交的數(shù)據(jù)....這里就不多說了...OK...
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Zend Framework框架教程之Zend_Db_Table_Rowset用法實(shí)例分析
- Zend Framework教程之Zend_Db_Table_Row用法實(shí)例分析
- Zend Framework教程之Zend_Db_Table用法詳解
- Zend Framework開發(fā)入門經(jīng)典教程
- Zend Framework框架Smarty擴(kuò)展實(shí)現(xiàn)方法
- Zend Framework框架路由機(jī)制代碼分析
- Zend Framework實(shí)現(xiàn)具有基本功能的留言本(附demo源碼下載)
- Zend Framework實(shí)現(xiàn)將session存儲在memcache中的方法
- Zend Framework分頁類用法詳解
- Zend Framework實(shí)現(xiàn)多文件上傳功能實(shí)例
- Zend Framework入門之環(huán)境配置及第一個Hello World示例(附demo源碼下載)
- Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法(附demo源碼下載)
- Zend Framework教程之Zend_Db_Table表關(guān)聯(lián)實(shí)例詳解

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

There are the following methods for front-end and back-end interaction using layui: $.ajax method: Simplify asynchronous HTTP requests. Custom request object: allows sending custom requests. Form control: handles form submission and data validation. Upload control: easily implement file upload.

Steps to build a single-page application (SPA) using PHP: Create a PHP file and load Vue.js. Define a Vue instance and create an HTML interface containing text input and output text. Create a JavaScript framework file containing Vue components. Include JavaScript framework files into PHP files.

The form tag is used to create a form that allows users to enter data and submit it to server-side processing. Attributes include action (handler URL), method (submission method), name (form name), target (submission target), enctype (data encoding method). Form elements include text boxes, drop-down lists, text areas, buttons, etc. Submitting the form will send the data to the server via the specified method and URL.

Servlet serves as a bridge for client-server communication in Java Web applications and is responsible for: processing client requests; generating HTTP responses; dynamically generating Web content; responding to customer interactions; managing HTTP session state; and providing security protection.

In Vue.js, event is a native JavaScript event triggered by the browser, while $event is a Vue-specific abstract event object used in Vue components. It is generally more convenient to use $event because it is formatted and enhanced to support data binding. Use event when you need to access specific functionality of the native event object.

DOM (Document Object Model) is an API for accessing, manipulating and modifying the tree structure of HTML/XML documents. It represents the document as a node hierarchy, including Document, Element, Text and Attribute nodes, which can be used to: access and modify Document structure Access and modify element styles Create/modify HTML content in response to user interaction
