Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/webobjects/woextensions/JSValidatedField.java


1   /*
2    * JSValidatedField.java
3    * © Copyright 2001 Apple Computer, Inc. All rights reserved.
4    * This a modified version.
5    * Original license: http://www.opensource.apple.com/apsl/
6    */
7   
8   package com.webobjects.woextensions;
9   
10  import com.webobjects.appserver.*;
11  import com.webobjects.foundation.*;
12  import java.util.Random;
13  
14  public class JSValidatedField extends WOComponent {
15      public String uniqueID;
16  
17      public JSValidatedField(WOContext aContext)  {
18          super(aContext);
19      }
20  
21      public void awake() {
22          // We need to give each image a unique name, with considerations that there might be
23          // more than ImageFlyover per page.
24          if (uniqueID == null) {
25              uniqueID = "Image"+(new Random()).nextInt();
26          }
27      }
28  
29  
30      public boolean synchronizesVariablesWithBindings() {
31          return false;
32      }
33  
34      public String validateFunction() {
35              // Return the name of the javascript function for the validation
36              return "validate_"+uniqueID+"()";
37      }
38  
39  
40      public String validationString() {
41  
42          String alertMessage;
43          String functionScript;
44          String addOr = null;
45  
46          // Start the function with it's name
47          functionScript = "function "+validateFunction()+" {\n \tif (";
48  
49          // Get the validation rules from the bindings; first check 'inputIsRequired' and add to the
50          // javascript function if it exists
51  
52          if (valueForBinding("inputIsRequired") != null) {  
53  
54              functionScript = functionScript + "document.forms."+valueForBinding("formName")+"."+uniqueID+".value == \"\"";
55  
56                  // Note that we already have one condition
57                  addOr = "YES";
58  
59          }
60  
61  
62          // Check for text that the user requires
63  
64          String requiredText = (String)_WOJExtensionsUtil.valueForBindingOrNull("requiredText",this);
65  
66          if (requiredText!=null) {
67              // Add the OR if we need it
68              if (addOr == "YES")
69                  functionScript = functionScript + " || ";
70  
71              functionScript = functionScript + "document.forms."+valueForBinding("formName")+"."+uniqueID+".value.indexOf(\""+requiredText+"\") == -1";
72  
73          }
74  
75          if (addOr =="YES") // none of the previous tests have fired
76              functionScript = functionScript +"0";
77          
78          // Check the user input for an alert message, and add it to the rules
79  
80          alertMessage = (String)_WOJExtensionsUtil.valueForBindingOrNull("errorMessage",this);
81          if (alertMessage==null)
82              alertMessage = "You have entered incorrect values -- please try again";
83  
84          functionScript = functionScript +")\n { alert(\""+alertMessage+"\"); return false; } }";
85  
86          // Return the function script
87          return functionScript;
88      }
89  }
90