This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Sep 27, 2012, 5:07 AM
41 Posts
topic has been resolvedResolved

Custom Field Validation

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.3
  • Role: Developer
  • Tags:
  • Replies: 4
Hi,
 
Am fairly new to XPages, and am putting in a Keyword document.  As part of the validation on the name, I want to check if the name already exists, if not present an error.  I've placed on the Validation tab of the Edit Control that it's a required field. I thought I'd use a CustomValidator to do the extra checking.  Below is the code I put in (following the same logic I used on the Notes form - Query Save).
 
var node=getComponent("inputText1");
var kwName=node.getSubmittedValue();
var vw:NotesView=database.getView("LkUpKewords");
var lkUpDoc:NotesDocument=vw.getDocumentByKey(kwName);

// No keyword found, so can exit, unique keyword
if(lkUpDoc==null)
{
    lkUpDoc.recycle();
    vw.recycle();
    return false;
}

// Lookup document found is the current document, so can exit
if(lkUpDoc.getNoteID()==document1.getNoteID())
{
    lkUpDoc.recycle();
    vw.recycle();
    return false;
}

// Found a different document, so fail the validation as keyword exists
lkUpDoc.recycle();
vw.recycle();
return "A keyword of that name already exists";
 
On Save I see in the Error Message Control - Error while executing JavaScript action expression.  Can anyone see what I've done wrong?  Thanks for any help
 
 
Cameron
Sep 27, 2012, 10:42 AM
129 Posts
Re: Custom Field Validation
I see two possible causes. If the document is not found, the value is null. null has no methods, hence null.recycle() will fail.
 
Not sure if validation expressions support strings as return value. It could be that it converts the string to a boolean true.
 
In querySave it's either no return statement (save runs), return false (save is stopped) or return true (save runs).
 
You don't need this in querySave if you have this as validation. 
 
Put the message string in the message-part of the expression validator. Either return true or false from the expression. 
 
I can't remember the types of validators, but expression validator seems like what you need. 
Oct 23, 2012, 10:26 PM
41 Posts
Re: Custom Field Validation
Hi Tommy,
 
Thanks for the reply, and apologies for not replying earlier.  I found the bug in the code, but the custom validator still doesn't work.  To try to narrow it down, I created a brand new XPage with just two fields, the Keyword Name and Keyword Value, plus an error control for each and a Save (Submit) button.  Just to refresh, I want to block the ability to create two keywords with the same name.
 
On the Keyword Name, I made it a mandatory field.  I also added the custom validator, plus put a mandatory validator on the keyword value.  If I leave a field blank, I get the popup error message that I put in the mandatory message. But, it seems like the custom validator runs AFTER the document has been saved, as checking a view in the background the document is there.  The message is displayed in the browser, but the fields have been cleared.  It does appear that the code works, it's just not running when it should - before the document is saved.  Is  there something I'm missing?
 
Again, thanks for any help.
 
Regards,
 
Cameron.
 
 
Oct 24, 2012, 4:53 AM
41 Posts
Re: Custom Field Validation
Hi Tommy,
 
 Finally fixed it - I changed it from a Custom Validator to an ExpressionValidator.  That worked.  Think I'll give up on custom validators.   Again, thanks for your help.
 
Regards,
 
Cameron
Oct 24, 2012, 1:28 PM
298 Posts
For the future and for everyone else, this is a great article on validators
http://www-10.lotus.com/ldd/ddwiki.nsf/dx/validation-on-an-xpage.htm

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal