« Learning XPages Part 32 : Using Roles To Make Fields Editable | Main| Learning XPages Part 34 : Server Side Validation »

Learning XPages Part 33⅓ : Simple Data Validation.

Tags : Lotus Domino XPages Validation
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

Now that we have given the ability for the end user to edit a document in their web browser we need to make sure that what they enter does not contain any invalid characters. An example of this might be the person's job title could be restricted to 50 characters, or phone numbers can only contain the +, - and numerics. There are two distinct ways to do data validation in XPages, one is client side and the other is server side. My preference is for client side validation as it does not involve the user having to send data to the server and then wait for it to come back but I will show you both methods so you can decided which option is right for you.

lets start by looking at the simple data validation options available in XPages. Start by opening our content_Person custom control again we will add a very simple validation to the Job Title field to make sure that it is filled in and that it is not more then 50 characters in length and not less then 10 characters.

Select the JobTitle field in the custom control and look down to the properties box below. Select the 'Validation' tab and then you can tick the 'required' box and fill in an error message to be displayed when the user doesn't fill in any entry for the field.

A picture named M2

To the right of this section you can see the string length validation properties :

A picture named M3

You may notice that these validation options all have the little diamond beside them. This means you can write JavaScript to compute the values for the fields if your application requires it. A good example is maybe you would want to turn off the 'required' part of a field for somebody with a 'DataManager' role in the ACL so that they can go in and quickly edit a document without having to make sure all the required fields have data in them.

So how does the XPage know that this is a string? Well if you look back on the 'Data' tab of the properties to the right there is a dropdown field to specify the type.

A picture named M4

Changing the type of the data field will show different options on the validation tab, for example if the field is a number then you can specify the range the numbers must be within, if the field is a date you can specify the date range the value must be within.

There are also a couple of hidden validation options that can be access through the 'All properties' tab under 'validators'. Here is what our current validation options entered about look like in here :

A picture named M5

If we save and refresh our XPage now and then go into edit mode and empty out the Job Title field and save the document you will see the web browser pop up with the error message we specified.

A picture named M6

This is an example of client side validation. In the next part I'll show you how to convert this to a server side validation so that you can see the difference.

NOTE : I have not covered creating the 'Save' action yet but it is as easy as duplicating the 'edit document' button and changing the simple action to save document, I will cover this button in a later part.
Download File xPhoneP33.zip

Comments

Gravatar Image1 - Declan - fantastic series and I love the 33 1/3. I can even hear you saying it Emoticon

Gravatar Image2 - As I've been reading the posts in the series, I have been speaking the text in my head as if you were presenting... have to say "33 1/3" every once in a while to get the correct pronunciation context Emoticon

You Rock Dec!

Gravatar Image3 - In the download file (xPhonep33.zip), the Person information no longer displays correctly in Firefox 2 (IE 7 is OK). The image placeholder and text to the right of the image are not displayed.

NOTE: This is an EXCELLENT tutorial, keep it up.

Gravatar Image4 - Hi,

i have been trying some validation, but I am doing a @DBlookup to get some values for other fields based on a name field. the problem I am having, is that the validation is kicking in BEFORE the @DbLookup is getting the chance to fill in the values.

Anyone know a way around this?

Thanks