198 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Domino Designer Java issues with FP9 ???
Thu, Sep 14th 2017 6
SOLUTION: Domino Server Setup Won’t Complete on Windows 2016
Mon, Aug 14th 2017 7
Okay now I’m ready (MWLUG slides posted)
Mon, Aug 7th 2017 4
Why You Should Go To MWLUG
Wed, Jul 26th 2017 6
MWLUG Session: Looking into Vue.js (through Domino goggles)
Tue, Jun 20th 2017 5
Like moving to a new house
Thu, Jan 12th 2017 7
Swiss Army Knife From Hell
Wed, Oct 19th 2016 11
Top 10
Some Solutions When ID Vault Won't Upload The Notes ID File
Mon, Apr 27th 2015 21
iNotes XSS Security errors after upgrade from Domino 8.5.3 to 9.0.1
Tue, Nov 19th 2013 13
Problem Installing Domino 9.0.1 FP3 on top of FP2 HF384
Tue, Feb 17th 2015 11
Quick Tip On Refreshing Templates
Wed, Jul 20th 2016 11
Swiss Army Knife From Hell
Wed, Oct 19th 2016 11
SSL Certificate Request Error - Invalid Keyfile Accessed
Tue, Jul 8th 2014 10
The State Of Web Development (good read)
Tue, Jun 28th 2016 7
Like moving to a new house
Thu, Jan 12th 2017 7
SOLUTION: Domino Server Setup Won’t Complete on Windows 2016
Mon, Aug 14th 2017 7
MWLUG 2016 – Worth the trip! and future directions
Thu, Aug 25th 2016 6


A Better Method For XPages SSJS Form Validation
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
Don Mottolo    

After that first tease of a post,  here's the real deal...   I just posted the technique I was alluding to on the OpenNTF XSnippets site:

XPages SSJS Form Validation That Triggers ErrorMessage Controls

http://openntf.org/XSnippets.nsf/snippet.xsp?id=ssjs-form-validation-that-triggers-errormessage-controls

Plus, here it is in full below.   Let me know what you think.

Don

 

Using the built in validation on XPages controls has the disadvantage that the validation logic gets scattered all over the XPage source.   While the other option of using SSJS for form validation does keep all the code in one place, but presents the problem of how to display the error messages back to the user.    This is the best way I've found so far to keep the code in one place for maintainability, while using my preferred method of displaying errors to the user right next to the field that they need to fix.   

Credit goes to Matt White for how to call validation in SSJS script library, to Andre Girrard for how to trigger the ErrorMessage control ( postValidationError() ), and to Tommy Valand for the $A() helper function, which allows my simple isEmpty() function to work properly with both single and multi-value fields.  The following technique pulls all those together.

1.  Create the SSJS script library  "xpValidation" using the code snippet (below).
2.  Add the  "xpValidation" SSJS script library as a resource to the XPage.
3.  Make the submit button's  "Button Type" =  "Button"   (not "Submit")
4.  In the button's OnClick event, click the "Add Group..." button, in the condition, call the validation function:  validateForm()
5.  Then inside the group, add a new "Save Data Sources" Simple Action   and set the "Name of page to open next" (This will only execute if the validation is passed.)

 

 

// This SSJS script library consolidates all the validation in one place
// The postValidationError() function flags a control as invalid and provides an error message
// so that the XPages ErrorMessage control is used to display the error on the page.
 
var validateForm = function(){
  var valid = true;
  var control;
  var val;
   
 
  // ***  REPEAT THE FOLLOWING BLOCK OF CODE FOR EACH CONTROL FOR BASIC "REQUIRED" VALIDATION
 
  // For each field, change the Control Name in getComponent() and the error message text in postValidationError()
  //   Optionally, modify the IF conditions with more complex JavaScript for value ranges, regular expressions, data lookups, etc.
 
  control = getComponent("title1");
  val = control.getValue();
  if (isEmpty(val)){
    valid = false;
    postValidationError(control,"Please enter a Title");
  }   
 
  // *** ----------------------------------------------------------------   *** 
 
 
 
 
  return valid;
}
 
function postValidationError(control, msg) {
    if ((typeof msg) != "string")
            return;
    var msgObj = new javax.faces.application.FacesMessage(javax.faces.application.FacesMessage.SEVERITY_ERROR, msg, msg);
    facesContext.addMessage(control.getClientId(facesContext), msgObj);
    control.setValid(false);
}
 
function isEmpty(o){
  return (o == null || @Trim($A(o)[0]) == "" ) ? true : false;
}
 
function $A( object ){
  try {
    if( typeof object === 'undefined' || object === null ){ return []; }
    if( typeof object === 'string' ){ return [ object ]; }
    if( typeof object.toArray !== 'undefined' ){return object.toArray();}
    if( object.constructor === Array ){ return object; }  
    return [ object ];
  } catch( e ) { Debug.exceptionToPage( e ); }
}


---------------------
http://www.bleedyellow.com/blogs/swissarmyknife/entry/xpages_ssjs_form_validation_that_triggers_errormessage_controls
Dec 14, 2011
4 hits



Recent Blog Posts
6
Domino Designer Java issues with FP9 ???
Thu, Sep 14th 2017 8:59p   Don Mottolo
I installed Notes Feature Pack 9 on my Windows 10 laptop the other day and have been running into various fluky issues.  Some custom toolbar buttons stopped working, so I tried running the FP9 installer again to uninstall it.   Even though it says it’s uninstalling it, when I restart Notes it is still on … Continue reading Domino Designer Java issues with FP9 ???
7
SOLUTION: Domino Server Setup Won’t Complete on Windows 2016
Mon, Aug 14th 2017 10:12p   Don Mottolo
A
4
Okay now I’m ready (MWLUG slides posted)
Mon, Aug 7th 2017 4:41p   Don Mottolo
Just posted the slides for my session (AD108 Looking Into Vue.js) at www.synap.com  which I’ll be presenting on Thursday. I’m batting clean-up  —  the last session of the last day and competing against Rob Novak’s ever-popular, “Free Beer’ session  (so, there may be an empty seat or two…               … Continue reading Okay now I’m ready (MWLUG slides posted)
6
Why You Should Go To MWLUG
Wed, Jul 26th 2017 9:34p   Don Mottolo
If you’re not familiar with the MWLUG conference (http://www.mwlug.com), it is great conference to attend if you want to: – get the maximum out of IBM collaboration software -OR- integrate with the latest technologies -OR- migrate away from Notes/Domino to other platforms Because the sessions are given by the people on the cutting edge of all these areas.   It is an … Continue reading Why You Should Go To MWLUG
5
MWLUG Session: Looking into Vue.js (through Domino goggles)
Tue, Jun 20th 2017 1:14p   Don Mottolo
Looking forward to presenting a session at MWLUG 2017 in Washington, D.C. Looking into Vue.js (through Domino goggles) Vue.js is rightfully getting tremendous buzz because of its power, speed, and simplicity.  It has been said that its developer, Evan You, has taken the best of Angular and React to create this new framework.  But for … Continue reading MWLUG Session: Looking into Vue.js (through Domino goggles)
7
Like moving to a new house
Thu, Jan 12th 2017 2:55p   Don Mottolo
A




Created and Maintained by Yancy Lent - About - Planet Lotus Blog - Advertising - Mobile Edition