Webx之表单验证

引入服务器端表单验证service,是通过在webx.xml中通过服务引入的方式完成的。例如,在user相关信息的表单验证的产生过程是这样的:webx-user.xml通过 <beans:import resource="user/form.xml" />引入form,xml表单验证配置,在form.xml中,通过services:form开始了表单的验证工作。

Webx表单验证服务主张验证逻辑和页面表现逻辑完全分离。所有的验证规则都写在一个单独的配置文件中 —— 页面模板是不需要关心这些验证规则的。当你需要修改验证规则时,只需要修改独立的配置文件就可以了,并不用修改页面模板。

开始配置表单验证服务。 
每个表单验证服务可包含多个groups。 
每个group可包含多个fields。 
每个field可包含多个validators。

user/form.xml具体配置如下: 
<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:services="http://www.alibaba.com/schema/services" 
    xmlns:fm-conditions="http://www.alibaba.com/schema/services/form/conditions" 
    xmlns:fm-validators="http://www.alibaba.com/schema/services/form/validators" 
    xmlns="http://www.alibaba.com/schema/services/form/validators" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
        http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd 
        http://www.alibaba.com/schema/services/form/conditions http://localhost:8080/schema/services-form-conditions.xsd 
        http://www.alibaba.com/schema/services/form/validators http://localhost:8080/schema/services-form-validators.xsd 
        http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd 
    ">

<services:form postOnlyByDefault="true"> 
        <!-- 
          - =============================================== 
          - 用来检查csrf token。 
          - =============================================== 
         --> 
        <services:group name="csrfTokenCheckGroup"> 
            <services:field name="csrfToken"> 
                <csrf-validator> 
                    <message>提交的数据已过期</message> 
                </csrf-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 登录表单 
          - =============================================== 
         --> 
        <services:group name="login" extends="csrfTokenCheckGroup"> 
            <services:field name="loginError"> 
                <custom-error id="invalidUserOrPassword"> 
                    <message>用户名或密码不正确,请重新输入</message> 
                </custom-error> 
            </services:field> 
            <services:field name="userId" displayName="登录名"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="password" displayName="密码"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册表单 
          - =============================================== 
         --> 
        <services:group name="register" extends="csrfTokenCheckGroup"> 
            <services:field name="registerError"> 
                <custom-error id="duplicatedUserId"> 
                    <message>登录名“${userId}”已经被人注掉了,请尝试另一个名字</message> 
                </custom-error> 
            </services:field> 
            <services:field name="userId" displayName="登录名"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <regexp-validator pattern="^[A-Za-z_][A-Za-z_0-9]*$"> 
                    <message>${displayName} 必须由字母、数字、下划线构成</message> 
                </regexp-validator> 
                <string-length-validator minLength="4" maxLength="10"> 
                    <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
                </string-length-validator> 
            </services:field> 
            <services:field name="password" displayName="密码"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <string-length-validator minLength="4" maxLength="10"> 
                    <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
                </string-length-validator> 
                <string-compare-validator notEqualTo="userId"> 
                    <message>${displayName} 不能与 ${userId.displayName} 相同</message> 
                </string-compare-validator> 
            </services:field> 
            <services:field name="passwordConfirm" displayName="密码验证"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <string-compare-validator equalTo="password"> 
                    <message>${displayName} 必须和 ${password.displayName} 相同</message> 
                </string-compare-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册account信息表单 
          - =============================================== 
         --> 
        <services:group name="registerContact" extends="csrfTokenCheckGroup"> 
            <services:field name="firstName" displayName="名字" propertyName="account.firstName"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="lastName" displayName="姓氏" propertyName="account.lastName"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="address1" displayName="地址" propertyName="account.address1"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="address2" displayName="地址" propertyName="account.address2"> </services:field> 
            <services:field name="city" displayName="城市" propertyName="account.city"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="zip" displayName="邮编" propertyName="account.zip"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <number-validator> 
                    <message>${displayName} 必须是数字</message> 
                </number-validator> 
            </services:field> 
            <services:field name="state" displayName="省份" propertyName="account.state"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="country" displayName="国家" propertyName="account.country"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="phone" displayName="电话" propertyName="account.phone"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="email" displayName="电子邮件" propertyName="account.email"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <mail-address-validator> 
                    <message>${displayName} 必须是合法的e-mail</message> 
                </mail-address-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册credit信息表单 
          - =============================================== 
         --> 
        <services:group name="registerCredit" extends="csrfTokenCheckGroup"> 
            <services:field name="creditCardNumber" displayName="信用卡号码" 
                propertyName="account.creditCardNumber"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <number-validator> 
                    <message>${displayName} 必须是数字</message> 
                </number-validator> 
            </services:field> 
            <services:field name="creditCardType" displayName="信用卡类型" 
                propertyName="account.creditCardType"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="creditCardExpiryMonth" displayName="信用卡过期月份" 
                propertyName="account.creditCardExpiryMonth"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="creditCardExpiryYear" displayName="信用卡过期年份" 
                propertyName="account.creditCardExpiryYear"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
        </services:group> 
    </services:form>

</beans:beans>

时间: 2024-11-08 23:51:09

Webx之表单验证的相关文章

python :表单验证--对每一个输入框进行验证

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"/> <title>Title</title> <style> .error{ color

jquery插件,表单验证validation plugin的使用

笔记: 一.首先引入插件 <script type="text/javascript" src="js/jquery-1.10.0.js" ></script> <script type="text/javascript" src="js/jquery.validate-1.13.1.js" ></script> 列子:一个简单的登录表单验证 1 <form id=&quo

AngularJs 表单验证

看到一些博客说这个框架要过时了,但是还是学习了下,觉的很方便的一个框架,有空的同学可以看看,适合我们入门看,比较基础. 对于日常的开发来说,最常见的开发场景就是通过表单编辑数据,这里涉及的问题就是验证问题. angularjs 内置已经支持了常见的验证方式,可以轻松实现表单验证. 1. 绑定 为了方便,我们在 $scope 上下文对象上创建一个 model 来表示我们编辑的内容. $scope.model = { id : 8, name: "alice", email: "

表单验证&amp;lt;AngularJs&amp;gt;

经常使用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,仅仅要在输入字段元素上加入HTML5标记required就可以: <input type="text" required />   2. 最小长度 验证表单输入的文本长度是否大于某个最小值,在输入字段上使用指令ng-minleng= "{number}": <input type="text" ng-minlength="5" /> 3.

AngularJS复习------表单验证

在AngularJS中能够将HTML5表单验证功能同自己的验证指令结合起来使用,这里介绍使用的核心功能. 使用表单验证,首先要确保表单的每个控件都有name属性 如果想要屏蔽浏览器对表单的默认验证行为,可以在表单元素上添加novalidate标记. 必填项:只需要在输入字段元素上添加HTML5标记 required即可 最小长度:在输入字段上使用AngularJS指令ng-minlength="{number}" 最大长度:在输入字段上使用AngularJS指令ng-maxlength

java 表单验证

1.思路:通过表单选择器,表单属性过滤器提取每个表单提交的值,进行验证 2.实现:javascript通过 onSubmit()事件,判断,返回值false不提交,返回true提交,jquery通过submit()事件 3.表单验证常用的方法和事件 a:事件 onblur 失去焦点, onfocus获得焦点 b:方法 blur() 移开焦点触发方法参数 focus() 在文本域中设置焦点 触发方法参数 select()选取文本域中的内容触发方法参数 4.正则表达式: a:定义 var reg =

struts2官方 中文教程 系列十一:使用XML进行表单验证

在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让您可以内置到Struts 2框架的验证器. 贴个本帖的地址,以免被爬:struts2官方 中文教程 系列十一:使用XML进行表单验证  即 http://www.cnblogs.com/linghaoxinpian/p/6938720.html 下载本章节代码 为了使用户能够编辑存储在Person对

angularjs中的表单验证

angular对html原生的form做了封装,增加了很多验证功能 1.代码结构 <form name="signup_form" novalidate ng-submit="signupForm()"> <div> <label>用户名</label> <input type="text" placeholder="Name" name="name"

第一篇,js表单验证模板

下面是对于一个email的表单验证的基本模板<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <style type="text/css" > .init{ border: 1px solid black; font-weight: bold; } .right{ border: 1px solid green; f