Webx之表单验证

 

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

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

user/form.xml具体配置如下:

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xmlns:services="http://www.alibaba.com/schema/services" 
  4.     xmlns:fm-conditions="http://www.alibaba.com/schema/services/form/conditions" 
  5.     xmlns:fm-validators="http://www.alibaba.com/schema/services/form/validators" 
  6.     xmlns="http://www.alibaba.com/schema/services/form/validators" 
  7.     xmlns:beans="http://www.springframework.org/schema/beans" 
  8.     xmlns:p="http://www.springframework.org/schema/p" 
  9.     xsi:schemaLocation=" 
  10.         http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd 
  11.         http://www.alibaba.com/schema/services/form/conditions http://localhost:8080/schema/services-form-conditions.xsd 
  12.         http://www.alibaba.com/schema/services/form/validators http://localhost:8080/schema/services-form-validators.xsd 
  13.         http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd 
  14.     "> 
  15.  
  16.     <services:form postOnlyByDefault="true"> 
  17.         <!-- 
  18.           - =============================================== 
  19.           - 用来检查csrf token。 
  20.           - =============================================== 
  21.          --> 
  22.         <services:group name="csrfTokenCheckGroup"> 
  23.             <services:field name="csrfToken"> 
  24.                 <csrf-validator> 
  25.                     <message>提交的数据已过期</message> 
  26.                 </csrf-validator> 
  27.             </services:field> 
  28.         </services:group> 
  29.         <!-- 
  30.           - =============================================== 
  31.           - 登录表单 
  32.           - =============================================== 
  33.          --> 
  34.         <services:group name="login" extends="csrfTokenCheckGroup"> 
  35.             <services:field name="loginError"> 
  36.                 <custom-error id="invalidUserOrPassword"> 
  37.                     <message>用户名或密码不正确,请重新输入</message> 
  38.                 </custom-error> 
  39.             </services:field> 
  40.             <services:field name="userId" displayName="登录名"> 
  41.                 <required-validator> 
  42.                     <message>必须填写 ${displayName}</message> 
  43.                 </required-validator> 
  44.             </services:field> 
  45.             <services:field name="password" displayName="密码"> 
  46.                 <required-validator> 
  47.                     <message>必须填写 ${displayName}</message> 
  48.                 </required-validator> 
  49.             </services:field> 
  50.         </services:group> 
  51.         <!-- 
  52.           - =============================================== 
  53.           - 注册表单 
  54.           - =============================================== 
  55.          --> 
  56.         <services:group name="register" extends="csrfTokenCheckGroup"> 
  57.             <services:field name="registerError"> 
  58.                 <custom-error id="duplicatedUserId"> 
  59.                     <message>登录名“${userId}”已经被人注掉了,请尝试另一个名字</message> 
  60.                 </custom-error> 
  61.             </services:field> 
  62.             <services:field name="userId" displayName="登录名"> 
  63.                 <required-validator> 
  64.                     <message>必须填写 ${displayName}</message> 
  65.                 </required-validator> 
  66.                 <regexp-validator pattern="^[A-Za-z_][A-Za-z_0-9]*$"> 
  67.                     <message>${displayName} 必须由字母、数字、下划线构成</message> 
  68.                 </regexp-validator> 
  69.                 <string-length-validator minLength="4" maxLength="10"> 
  70.                     <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
  71.                 </string-length-validator> 
  72.             </services:field> 
  73.             <services:field name="password" displayName="密码"> 
  74.                 <required-validator> 
  75.                     <message>必须填写 ${displayName}</message> 
  76.                 </required-validator> 
  77.                 <string-length-validator minLength="4" maxLength="10"> 
  78.                     <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
  79.                 </string-length-validator> 
  80.                 <string-compare-validator notEqualTo="userId"> 
  81.                     <message>${displayName} 不能与 ${userId.displayName} 相同</message> 
  82.                 </string-compare-validator> 
  83.             </services:field> 
  84.             <services:field name="passwordConfirm" displayName="密码验证"> 
  85.                 <required-validator> 
  86.                     <message>必须填写 ${displayName}</message> 
  87.                 </required-validator> 
  88.                 <string-compare-validator equalTo="password"> 
  89.                     <message>${displayName} 必须和 ${password.displayName} 相同</message> 
  90.                 </string-compare-validator> 
  91.             </services:field> 
  92.         </services:group> 
  93.         <!-- 
  94.           - =============================================== 
  95.           - 注册account信息表单 
  96.           - =============================================== 
  97.          --> 
  98.         <services:group name="registerContact" extends="csrfTokenCheckGroup"> 
  99.             <services:field name="firstName" displayName="名字" propertyName="account.firstName"> 
  100.                 <required-validator> 
  101.                     <message>必须填写 ${displayName}</message> 
  102.                 </required-validator> 
  103.             </services:field> 
  104.             <services:field name="lastName" displayName="姓氏" propertyName="account.lastName"> 
  105.                 <required-validator> 
  106.                     <message>必须填写 ${displayName}</message> 
  107.                 </required-validator> 
  108.             </services:field> 
  109.             <services:field name="address1" displayName="地址" propertyName="account.address1"> 
  110.                 <required-validator> 
  111.                     <message>必须填写 ${displayName}</message> 
  112.                 </required-validator> 
  113.             </services:field> 
  114.             <services:field name="address2" displayName="地址" propertyName="account.address2"> </services:field> 
  115.             <services:field name="city" displayName="城市" propertyName="account.city"> 
  116.                 <required-validator> 
  117.                     <message>必须填写 ${displayName}</message> 
  118.                 </required-validator> 
  119.             </services:field> 
  120.             <services:field name="zip" displayName="邮编" propertyName="account.zip"> 
  121.                 <required-validator> 
  122.                     <message>必须填写 ${displayName}</message> 
  123.                 </required-validator> 
  124.                 <number-validator> 
  125.                     <message>${displayName} 必须是数字</message> 
  126.                 </number-validator> 
  127.             </services:field> 
  128.             <services:field name="state" displayName="省份" propertyName="account.state"> 
  129.                 <required-validator> 
  130.                     <message>必须填写 ${displayName}</message> 
  131.                 </required-validator> 
  132.             </services:field> 
  133.             <services:field name="country" displayName="国家" propertyName="account.country"> 
  134.                 <required-validator> 
  135.                     <message>必须填写 ${displayName}</message> 
  136.                 </required-validator> 
  137.             </services:field> 
  138.             <services:field name="phone" displayName="电话" propertyName="account.phone"> 
  139.                 <required-validator> 
  140.                     <message>必须填写 ${displayName}</message> 
  141.                 </required-validator> 
  142.             </services:field> 
  143.             <services:field name="email" displayName="电子邮件" propertyName="account.email"> 
  144.                 <required-validator> 
  145.                     <message>必须填写 ${displayName}</message> 
  146.                 </required-validator> 
  147.                 <mail-address-validator> 
  148.                     <message>${displayName} 必须是合法的e-mail</message> 
  149.                 </mail-address-validator> 
  150.             </services:field> 
  151.         </services:group> 
  152.         <!-- 
  153.           - =============================================== 
  154.           - 注册credit信息表单 
  155.           - =============================================== 
  156.          --> 
  157.         <services:group name="registerCredit" extends="csrfTokenCheckGroup"> 
  158.             <services:field name="creditCardNumber" displayName="信用卡号码" 
  159.                 propertyName="account.creditCardNumber"> 
  160.                 <required-validator> 
  161.                     <message>必须填写 ${displayName}</message> 
  162.                 </required-validator> 
  163.                 <number-validator> 
  164.                     <message>${displayName} 必须是数字</message> 
  165.                 </number-validator> 
  166.             </services:field> 
  167.             <services:field name="creditCardType" displayName="信用卡类型" 
  168.                 propertyName="account.creditCardType"> 
  169.                 <required-validator> 
  170.                     <message>必须填写 ${displayName}</message> 
  171.                 </required-validator> 
  172.             </services:field> 
  173.             <services:field name="creditCardExpiryMonth" displayName="信用卡过期月份" 
  174.                 propertyName="account.creditCardExpiryMonth"> 
  175.                 <required-validator> 
  176.                     <message>必须填写 ${displayName}</message> 
  177.                 </required-validator> 
  178.             </services:field> 
  179.             <services:field name="creditCardExpiryYear" displayName="信用卡过期年份" 
  180.                 propertyName="account.creditCardExpiryYear"> 
  181.                 <required-validator> 
  182.                     <message>必须填写 ${displayName}</message> 
  183.                 </required-validator> 
  184.             </services:field> 
  185.         </services:group> 
  186.     </services:form> 
  187.  
  188. </beans:beans> 

  

你可能感兴趣的:(职场,webx,表单验证,休闲)