Email Address Validator for XPages

Update: 13 Feb 2015 – The instructions for installing into an NSF were ambiguous because I referred to ‘the <validator> section’ when there is in fact 2 different ‘<validator>’ sections (one in faces-config and one in the xsp-config’ I have made this section clearer!

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.

But I like things to be even simpler, even if it means going through some pain to get there! 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.

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.

Faces Config vs Xsp Config

Before we get into the next section I just want to point out the 2 different types of configuration that we will use for configuring the validator, Faces Configuration and Xsp Configuration.
Faces Config files and Xsp-Config files both look very similar but they are not the same thing and it is important to put the correct configuration in the correct type of config file.

  • Faces Config is used at runtime (when your application starts up) to configure your application, Faces Config files use the extension ‘.xml
  • Xsp Config is used at design time by Domino Designer to configure Domino Designer to understand what controls / components are available and how they can be edited. Xsp Config files use the file extension ‘.xsp-config

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

emailValidator-faces-config.xml

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:

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

Ok great now lets check the validators picklist:

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

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. Update your faces config to include the <validator>…</validator> section of the above code sample titled ’emailValidator-faces.config.xml’. You can do this by going to ‘Application Configuration’ -> faces-config section of the NSF.
  3. Using the ‘Package Explorer’ window in Designer, create a file called emailValidator.xsp-config in the WebContentWEB-INF directory of the NSF, and put the contents as shown in the code sample titled ’emailValidator.xsp-config’. (it can be named anything e.g. foo.xsp-config but it should have the extension .xsp-config)

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 download! 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 predefined 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!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *