OMNICLIENT VALIDATION, PART 1

Part one of two, in a series.

The simple premise is that I’d like to be able to write a client-side form validation once in (Lotus) Domino Designer and run it anywhere. Sounds like a recipe for JavaScript, right? Ah, but there’s a catch. A few catches to be precise. First off, AFAIK JavaScript version support in the Notes client has been frozen at about 1.3 since R5. What that means for practical purposes is that I was unable to use the Object.hasOwnProperty method in my validation object (more on that later.) Also, the Notes client doesn’t support DHTML-style rendering in JavaScript to place field validation messages directly on the form as is common practice in webforms.

To begin with, I created a simple JavaScript validation object that will hold all validation logic. The object has arrays for individual field validation rules, an array for messages to display if the validation fails for any of the fields, a boolean pass or fail property, a property that contains the field to focus on in a failure event (the first field to fail validation) and some other housekeeping properties:

  1. function ValidationObject(aform){
  2. this.form = aform;
  3. this.isValid;
  4. this.focusOn = null;
  5. this.validationMessages;
  6. this.errorMessage
  7. this.fieldValidations
  8. this.setValidations = oSetValidations;
  9. this.validate = oValidate;
  10. }//ValidationObject

ValidationObject has two methods, setValidations, which populates the individual validation rules for a form (the aforementioned array) and validate, which performs the validation, returning true or false for pass/fail. The real interesting logic is in setValidations:

  1. //rule array is 2 dimensional array.
  2. //inner array is 3 or 4 elements, 4th is optional
  3. //element 1: field name (HTML name)
  4. //element 2: field type (String) {TEXT|EMAIL|DATE|CHECK|RADIO}
  5. //element 3: failure message
  6. //element 4: optional arguments key:value object (ex: {emptyOk: true})
  7. function oSetValidations(vRuleArr){
  8. if (vRuleArr.constructor != Array) {
  9. this.errorMessages = "validation rules error: not an array";
  10. return false;
  11. }
  12. for (i = 0;i<vRuleArr.length;i++){
  13. var ruleArr = vRuleArr[i];
  14. if ((ruleArr.constructor != Array) || (ruleArr.length < 3) || (ruleArr.length > 4)){
  15. this.errorMessage = "validation rules error: rule " + i + 1;
  16. return false;
  17. }
  18. }
  19. this.fieldValidations = vRuleArr;
  20. return true;
  21. }//oSetValidations

Each field on the form to be validated is defined by a rule array that is an element in the vRuleArr array parameter. Standard stuff, the parameters are defined in the comments above the code snippet above, no need to repeat myself. The ast parameter, an optional Object literal containing key:value pairs that the validation logic can use to branch on, needed to be backported to the JS that Notes supports due to the aforementioned Object.hasOwnProperty issue. To solve, I was able to use Object.propertyname == undefined to perform the same logic in a Notes client-friendly manner. A snippet of the validate method highlights:

  1. function oValidate(){
  2. this.isValid = true;
  3. this.validationMessages = new Array();
  4. for (i = 0;i<this.fieldValidations.length;i++){
  5. var rule = this.fieldValidations[i];
  6. var hasOpts = (rule.length == 4) ? rule[3] : false;
  7. var fld = eval("this.form." + rule[0]);
  8. if (rule[1] == "TEXT"){
  9. emptyOk = (hasOpts && (hasOpts.emptyOk != undefined) && hasOpts.emptyOk) ? true : false;
  10. if ((fld.value.split(' ').join('') == "") && (!emptyOk)) {
  11. if (this.isValid) this.focusOn = fld;
  12. this.isValid = false;
  13. this.validationMessages.push(rule[2]);
  14. }
  15. }
  16. else if (true) {
  17. //MORE VALIDATION TYPES………….
  18. }
  19. }
  20. if (!this.isValid){
  21. alert(this.validationMessages.join('n'));
  22. this.focusOn.focus();
  23. }
  24. return this.isValid
  25. }//oValidate

Line 9 is where I needed to query the optional arguments object for the text validation logic. All this is leading up to a larger issue of how to perform a simple submit in the Notes client, contingent on validation and close the document using as much common code as possible for Notes and the web. Stop me if you’ve heard this before. To be continued. Please chime in with your feedback.