Shale Validate 机制初探

Validation Introduction

JavaServer Faces 1.x does not explicitly support client-side validation and only provides a minimal set of server-side validators. On the other hand, Apache Commons Validator supports both client- and server-side validators and comes with the following useful validators:

  • Credit Card
  • Date
  • Email
  • Generic
  • ISBN
  • URL
All of the preceding validators can be executed either on the client or the server, or both. Shale integrates JavaServer Faces and the Commons Validator.
For example:
   
Server Validate Demo:
  1. <h:inputText     id="amount"  
  2.                    value="#{validate$test.amount}"  
  3.                     size="7">  
  4.   
  5.         <f:convertNumber  
  6.        minFractionDigits="2"/>  
  7.   
  8.         <val:commonsValidator  
  9.                     type="floatRange"  
  10.                      min="10"  
  11.                      max="1000"  
  12.                      arg="#{messages['validate.test.amount']}"  
  13.                   server="true"  
  14.                   client="false"/>  
  15.   
  16.      </h:inputText>  

Client side Validate demo:

  1. <h:inputText     id="date"  
  2.                      value="#{validate$test.expirationDate}">  
  3.   
  4.          <f:convertDateTime  
  5.                     pattern="MM/dd/yyyy"/>  
  6.   
  7.            <val:commonsValidator  
  8.                        type="required"  
  9.                        arg="#{messages['prompt.expirationDate']}"  
  10.                     server="false"  
  11.                     client="true"/>  
  12.   
  13.          <val:commonsValidator  
  14.                       type="date"  
  15.          datePatternStrict="MM/dd/yyyy"  
  16.                    message="#{messages['validate.test.bad.expiration.date']}"  
  17.                        arg="#{messages['prompt.expirationDate']}"  
  18.                     server="false"  
  19.                     client="true"/>  
  20.   
  21.       </h:inputText>  

 

Services Provided

Shale provides three JSP tags that let you use the Commons Validator: val:commonsValidator, val:validatorVar, and val:validatorScript. The first two lets you attach a commons validator to a JSF input component and the third generates JavaScript validation code for validating each JSF component that has one or more Commons validators in a particular form. You can attach as many Commons validators to a single JSF input component as you wish.

Using Commons Validator Integration

Here's what you need to do to use Shale validation:

  • Add an onsubmit attribute to your h:form tag that calls the JavaScript validation function generated by val:validatorScript.
  • Add Commons validators to JSF input components with val:commonsValidator and, optionally, val:validatorVar.
  • Add an val:validatorScript tag at the end of the h:form tag's body.

Here's an example:

  1. <%@ taglib uri="http://shale.apache.org/core" prefix="val" %>  
  2. ...   
  3. <h:form onsubmit="return validateForm(this);">  
  4.   
  5.     <h:inputText id="creditCardNumber"    
  6.                size="16"  
  7.               value="#{userContext.creditCardNumber}">  
  8.   
  9.         <val:commonsValidator type="required"  
  10.                              arg="#{msgs.creditCardNumberPrompt}"  
  11.                           server="true"  
  12.                           client="true"/>  
  13.   
  14.         <val:commonsValidator type="mask"  
  15.                              arg="#{msgs.creditCardNumberPrompt}"  
  16.                           server="true"  
  17.                           client="true">  
  18.             <val:validatorVar name="mask" value="[4-6].*"/>  
  19.         </val:commonsValidator>  
  20.   
  21.         <val:commonsValidator type="creditCard"    
  22.                              arg="#{msgs.creditCardNumberPrompt}"    
  23.                           server="true"/>  
  24.     </h:inputText>    
  25.   
  26.     <h:message for="creditCardNumber" styleClass="errors"/>    
  27.   
  28.     <val:validatorScript functionName="validateForm"/>  
  29. </h:form>  

   In the preceding example, we've attached three Commons validators to a single JSF input component. To pass validation, the field must have a value that starts with a number between 4 and 6 inclusive and that value must be a valid credit card number as verified by the Luhn algorithm. Two of the validations are performed on both client and server and one is performed on the server only.

  Note: At the present time, you have the option to forego server-side validation, which is considered very bad practice. Users can turn off JavaScript, so you should always backup client-side validation with server-side validation. In the future, Shale may enforce server-side validation if it's not explicitly specified.

你可能感兴趣的:(JavaScript,apache,jsp,F#,JSF)