199 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Bundle Inspector – Diagnosing XPages Plugin Resolution problems
Wed, Aug 2nd 2017 1
Swiper Official Version 2 Release
Fri, Jul 21st 2017 3
Auto-width Bootstrap Column XPages Controls
Tue, Mar 21st 2017 1
Swiper FP8 Version Beta Release
Thu, Mar 16th 2017 4
Swiper FP8 Integration Rollout
Wed, Mar 8th 2017 5
Markdown XPages UIControl
Sun, Mar 5th 2017 5
TextDiff XPages control – For visual comparison of text
Tue, Feb 28th 2017 2
Top 10
Inspect Component Properties using Component Binding
Tue, Mar 8th 2016 9
Build System for XPages and OSGi plugins
Fri, Aug 8th 2014 8
Extended Messages Control available as an XspLibrary
Tue, Jul 12th 2016 8
Pasting Images into XPages CKEditor
Sun, Feb 12th 2017 6
XPages webmail – Using Mime Inspector to debug Mime
Tue, Feb 14th 2017 6
Markdown XPages UIControl
Sun, Mar 5th 2017 5
Swiper FP8 Integration Rollout
Wed, Mar 8th 2017 5
XPages PhoneNumber Control - Part 2 Upgrades to the Converter
Mon, Feb 23rd 2015 4
Uploading Plugins Headlessly to Open Eclipse Update Site
Sat, Jul 18th 2015 4
Uploading Plugins Headlessly to Open Eclipse Update Site
Sat, Jul 18th 2015 4


Email Address Validator for XPages
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
   

I wanted to implement a custom validator for validating the input of an email address.

There are several ways this could be done, and a normal person would just use some sort of regular expression and a  xp:validateConstraint validator, you could also do it in a xp:validateExpression.

Clearly I am not normal because I was curious to see how can I make my own custom validator that can be reused project to project, served up through a plugin.

I also wanted to be able to select it from this validators list in the control properties box.



To get me started I found a blog post from Jeremy Hodge a few years back.
Jeremy's post was just what I needed to get started. Jeremy describes 3 methods to use a custom validator's. I'll let you read the full article regarding the methods because there is a bit of detail, but I decided to go with the method where you create your own class that implements javax.faces.validator.Validator and register it through the faces config file.

Create the Validator

Here is the validator I created, my EmailAddressValidator! It is pretty simple, it just has to implement the validate function. The parameter  'Object o' is the value that is being validated, we turn it into a string and validate it using a regex pattern.
The validate function should throw a ValidatorException if validation has failed.

package com.gregorbyte.xsp.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.ibm.commons.util.StringUtil;

public class EmailAddressValidator implements Validator {

private final static String EMAIL_PATTERN = "[^\s@]+@[^\s@]+\.[^\s@]+";
private final static Pattern EMAIL_COMPILED_PATTERN = Pattern
.compile(EMAIL_PATTERN);

public void validate(FacesContext context, UIComponent component, Object o)
throws ValidatorException {
if (o == null || StringUtil.isEmpty((String) o)) {
return;
}
String trimmed = ((String) o).trim();
Matcher matcher = EMAIL_COMPILED_PATTERN.matcher(trimmed);
if (!matcher.matches()) {
FacesMessage msg = new FacesMessage("Invalid email value!",
"Supplied Email address is not in a valid format");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}

}


So now we can have and endless discussion about which regex pattern should be used but the one I am using basically is anythingbutwhitespace@anythingbutwhitespace.anythingbutwhitespace (any can have extra (.anythingbutwhitespace)'s as needed. You could replace it with whatever you like but this is good enough for me.

Register the Validator with Faces Config

So at this point I can use the validator using the <xp:validator> tag and reference the validator using a validator id that I register with faces config.
Here is the part where I register it with faces-config

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<validator>
<validator-id>gregorbyte.EmailAddressValidator</validator-id>
<validator-class>com.gregorbyte.xsp.validator.EmailAddressValidator</validator-class>
</validator>
</faces-config>

So now that the validator is registered through faces config with a validatorId, I can use it on an XPage with the xp:validator tag, here is an example XPage:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:gb="http://www.gregorbyte.com/xsp/">

<xp:inputText id="inputText1">
<xp:this.validators>
<xp:validator validatorId="gregorbyte.EmailAddressValidator"></xp:validator>
</xp:this.validators>
</xp:inputText>

<xp:message id="message1" for="inputText1"></xp:message>

<xp:button value="Submit" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
</xp:eventHandler>
</xp:button>

</xp:view>

And here is the result:


Not good enough for me! I hate typing the Validator Id

Ok great! But now I am still not satisfied, I want to be able to select the validator from the previously mentioned validators box. The secret here is to create an xsp-config file to describe this extension so that Domino Designer knows that it is available.
All the details for this can be found on the domino developer wiki under the Xpages Configuration File Format section. Here is the section for a validator

My validator doesn't have any properties at the moment. So I'll leave out the <property> elements and just fill out the other parts:

Register the validator via Xsp-Config

Here is my xsp-config file, notice the prefix / uri section at the top as well. The default-prefix combined with the validator-extension > tag-name will be what my validator will show up as by default. e.g. <gb:emailAddressValidator>

emailValidator.xsp-config

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>

<faces-config-extension>
<namespace-uri>http://www.gregorbyte.com/xsp/</namespace-uri>
<default-prefix>gb</default-prefix>
</faces-config-extension>

<validator>
<description>Validates that input is in the format of an Email Address</description>
<display-name>Email Address Validator</display-name>
<validator-id>gregorbyte.EmailAddressValidator</validator-id>
<validator-class>com.gregorbyte.xsp.validator.EmailAddressValidator</validator-class>
<group-type-ref>com.ibm.xsp.validator.group.Validator</group-type-ref>
<validator-extension>
<tag-name>emailAddressValidator</tag-name>
</validator-extension>
</validator>

</faces-config>

Ok great now lets check the validators picklist:


And now here is a new example Xpage that uses the gb:emailAddressValidator tag:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:gb="http://www.gregorbyte.com/xsp/">

<xp:label value="Using gb:emailAddressValidator" id="label1"
for="inputText1">
</xp:label>

<xp:inputText id="inputText1">
<xp:this.validators>
<gb:emailAddressValidator></gb:emailAddressValidator>
</xp:this.validators>
</xp:inputText>

<xp:message id="message1" for="inputText1"></xp:message>

<xp:button value="Submit" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
</xp:eventHandler>
</xp:button>

</xp:view>

And here is the validator in action:



Where do you put all the files? How do you deploy it?

Ok so here you have 2 options, you can just put the necessary files in the NSF that you want to use it with, or you can put it in an OSGi plugin to be installed to Domino Designer, and to any Domino Server that uses it

Putting necessary files in the same NSF

This is the easiest method, but you would need to do it for every NSF that you want to use the validator.
  1. Put the Java class EmailAddressValidator in the Code -> Java section of the NSF
  2. Include the <validator>...</validator> section (from the 'faces config' above) in the Application Configuration -> faces-config section of the NSF
  3. Using the 'Package Explorer' window, create a file called emailValidator.xsp-config in the WebContentWEB-INF directory of the NSF, and put the contents as shown in my

Deploying via a Plugin

This is a whole article in itself! Here are the broad 'checklist' items (from the top of my head), it won't be enough detail for you to do it yourself so I created the plugin and put it on github under camac/XPagesEmailValidatorPlugin. Go to the releases section and downlad! Let me know via github issues if any problems!
  1. Create a plugin project
  2. Put the emailValidator.xsp-config file somewhere
  3. create emailValidator-faces-config.xml file somewhere
  4. Put the EmailValidator class somewhere
  5. Add com.ibm.commons and com.ibm.xsp.core to required bundles
  6. Create an 'EmailValidatorXspLibrary' that extends AbstractXspLibrary
  7. Configure the new EmailValidatorXspLibrary to serve up the xsp config and faces config files (via overriding getFacesConfigFiles and getXspConfigFiles methods)
  8. Configure the other details of the library such as the plugin name, library id, and whether it is global scope (all NSF use automatically) or not global (must select to use library for each nsf)
  9. Create a new extension that extends 'com.ibm.commons.Extension' with the type 'com.ibm.xsp.Library' and select the class 'EmailValidatorXspLibrary'
  10. Make sure the plugin is configured (via Manifest.MF) to export whatever packages that the EmailValidatorXspLibrary  and EmailValidator are contained in (this is common oversight!)
  11. Create a feature project that includes this plugin
  12. Create an update site project that includes the feature
  13. Build the Update site
  14. Deploy to Domino Designer
  15. Deploy to Domino Server via UpdateSite.nsf or similar!

Conclusion

So I am very happy with the result, I have my EmailValidator in a plugin, I can select the EmailValidator from the validators drop down. If I get into an argument about the best Regex to use to validate emails, and I end up losing the argument, I can update the regex in one spot, redeploy and all NSF's on the server will use the new Regex! I can even add properties to choose between different strictness of predifined regex's if I went down that path.

I hope you enjoyed this post and got something out of it, let me know if you have any questions!




---------------------
http://www.gregorbyte.com/2015/02/email-address-validator-for-xpages.html
Feb 11, 2015
4 hits



Recent Blog Posts
1
Bundle Inspector – Diagnosing XPages Plugin Resolution problems
Wed, Aug 2nd 2017 12:26p   Cameron Gregor
A common task with XPages development is to installing some plugins that provide additional functionality. If you do any plugin development of your own, you end up doing this task a lot more as you create new plugins, install new versions etc. It is also common to encounter some problems after installing plugins to your Domino Server! Maybe your expected plugin doesn’t load up at all? maybe the wrong version is loading? The standard method of diagnosing these problems is to use the OSGi c
3
Swiper Official Version 2 Release
Fri, Jul 21st 2017 1:52p   Cameron Gregor
So I have finally posted Swiper version 2 to OpenNTF! (and Github of course) Swiper OpenNTF Project Page Swiper Github Releases The latest version is 2.0.1,  and is the same as 2.0.0beta but with a bug fix for the toolbar buttons. Swiper 2.0.0 beta has been available on the Github project site for a few months, and the core functionality of it works as planned. There was however a bug within the shortcut buttons that I added to the menu bar, and this is a bit of a nasty bug that can cause del
1
Auto-width Bootstrap Column XPages Controls
Tue, Mar 21st 2017 1:13p   Cameron Gregor
I’ve been stuck working with OneUI Version 3 for the past couple of years, due to a regretful decision made at the beginning of my major project. OneUI was better than nothing but very frustrating at times. Finally, I have moved on to my next project and I am now using bootstrap (version 3) A common task when laying out a page using bootstrap is to divide sections up into rows and columns, and use the appropriate css styles to do so. I’m going to assume you are familiar with bo
4
Swiper FP8 Version Beta Release
Thu, Mar 16th 2017 12:50p   Cameron Gregor
Last week I released the ‘alpha’ version of Swiper which was untested on FP8 but presumed to be ok. So far I have only had good reports from the pioneers who have gone ahead and installed FP8 + the alpha version. I have since managed to ugrade my home office setup to FP8 which unfortunately has broken my ability to launch designer from eclipse but I am seeking some advice on fixing this up. In the meantime I have to test the slow way of building plugins, import plugins, restart R
5
Swiper FP8 Integration Rollout
Wed, Mar 8th 2017 12:43p   Cameron Gregor
Notes Domino 9.0.1 FP8 is finally here and as far as I know (I have yet to download it) it includes the necessary changes which will allow Swiper to swipe whatever it wants, whenever it wants, which is good news for people who don’t like to have ‘Build Automatically’ turned on. Plan of Attack for release of Swiper version 2.0.0 So, I haven’t actually downloaded FP8 yet,  so I can’t say for sure that the updated version works perfectly. Here is a bit of backgrou
5
Markdown XPages UIControl
Sun, Mar 5th 2017 11:44a   Cameron Gregor
Often when I’m designing an xpage, there might be a section of the page in which I want to explain some instructions to the user. Some options here are to: write the Instructions using html and embed directly in the xpage markup write the Instructions directly in the design pane and format using designer’s ui e.g. bold, color, size etc use some native xpage controls to achieve the desired result. Write the instructions in a richtext field on a notes document that is loaded to dis
2
TextDiff XPages control – For visual comparison of text
Tue, Feb 28th 2017 12:00p   Cameron Gregor
A few years back I stumbled across Google’s diff-match-patch project which provides some handy algorithms for text manipulation. At the time of discovery I was doing ‘classic’ notes development. Although I probably could have implemented something that worked in lotuscript with RichText or Mime, it wasn’t a priority at the time and I never bothered. Since then, I have been doing mainly XPages, and now that I have been also doing a bit of XPages Control development. I was
6
XPages webmail – Using Mime Inspector to debug Mime
Tue, Feb 14th 2017 11:12a   Cameron Gregor
In a previous post in this series I did a bit of an overview on how MIME works. We also did a little bit about how MIME works in XPages + Domino land. With this knowledge in hand we can now start to analyse the different ways a ‘Pretty words, pictures and attachments’ can be stored in the document. During development of the ‘XPages Webmail’ interface, I encountered many problems which could only be solved by investigating the MIME content in detail. To help me do this, I
6
Pasting Images into XPages CKEditor
Sun, Feb 12th 2017 10:00p   Cameron Gregor
Programs like ‘Snipping Tool’ on Windows, are super useful for users to make a quick snapshot, do some quick markup on the image, paste into chat/email and send. Unfortunately when using the default configuration of CKEditor in XPages (the inputRichText control), support for pasting images is not available for all browsers, and even for the ones that do support it, the images are only pasted as a PNG data URI. I have explained data URI images in a previous post, so check that out if
4
Preventing pasting of remotely hosted images in CKEditor
Mon, Nov 14th 2016 11:21p   Cameron Gregor
In the previous post, I showed how to prevent a user from pasting Images from the Clipboard into CKEditor. This post is of a similar nature but is designed to ensure that users don’t paste images with URLs to external / internal applications. This post is part of my XPages webmail tips series, and addresses a problem where, a user copies and pastes some HTML that includes images, from a webpage and pastes it into CKEditor for a message that is then sent via email. The recipient is then una




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