Option Four

Option Four seems quite convoluted, but is essentially a simple principle. We use server-side javascript in the beforePageLoad event to set viewScope variables for each of the radio button, check box and list box mandatory questions we want to check against. We then use the onClick event to trigger a partial refresh and clear the viewScope variables. Our validation again uses server-side javascript to check the viewScope variable, and set a second viewScope variable if validation fails. We then set the styling of a div based on this second viewScope variable.

A few basic points to note. The beforeRenderResponse event of the XPage forces IE7 mode for Internet Explorer 8, which is required with XPages in Domino 8.5 because Dojo framework 1.1 is used (8.5.1 uses Dojo framework 1.3.2, which is supported by IE8). This is irrelevant for this example, but is useful for anyone still coding on 8.5. The basic theme is used for the whole application, defined in the Application Properties, and this ensures styles.css stylesheet is used. Themes are worth getting to grips with, if you haven’t already, because they add a lot of flexibility.

The structure is the same as option3.xsp. Our XPage, option4.xsp, includes a custom control for each answer document, answerDoc_8_5_1, which has custom controls for each answer type. For the three standard ones we use the same custom controls used by Option 3 – convertValidationTextBox, convertTextBox and convertDropDown. For the others we use convertRadio, convertCheckBox and convertMultiSelect.

option4.xsp includes the option4Validation.js (the server-side validation script library) and option4CSValidation.js (a client-side validation script library that does the validation for convertValidationTextBox, convertTextBox and convertDropDown). The function in option4CSValidation.js is just a cut-down version of the same function in option3Validation.js. The beforePageLoad event calls the loadMandatory() function in option4Validation.js, and the ‘Submit Answers’ button validates using the validationSpecial() in option4.js. Cutting it down to its basics, the loadMandatory() function is this:

var useView:NotesView = database.getView(“AnswerDocs851”);

var ec:NotesViewEntryCollection = useView.getAllEntries();

var entry:NotesViewEntry = ec.getFirstEntry();

while (entry != null) {

if (entry.getColumnValues()[6] != “”) {

if (entry.getDocument().getItemValueString(“Response”) == “”) {

viewScope.put(@Left(entry.getColumnValues()[6], “~”), @Right(entry.getColumnValues()[6], “~”))

}

}

entry = ec.getNextEntry();

}

We get the view, then check column 6. If the question is due to be mandatory, this is the NoteID + “~” + error message. So we create a viewScope variable with the NoteID, whose value is the error message, like so.

Initially loaded viewScopes

It would be much easier to set the viewScope if required using the beforePageLoad of the answerDoc_8_5_1 custom control. However, if you try using ansDoc in any of the events, it throws a javascript error, as below. It is only possible to do so if repeatControls property of the repeat is set to True, as I mentioned in Part Five.

If we now look at the convertRadio custom control, this has a computed field (not rendered) that has the NoteID as the value. This is used in the script block to attach the error tooltip. Our radioGroup control is within a div whose id is the document’s NoteID with its class set to errorDiv if we have a viewScope variable called NoteIDError. We then have the radioGroup with an onClick event to clear the viewScope variables called NoteID and NoteIDError. The important point here is that it does a partial refresh with immediate set to true. This is very important, otherwise if you click a radio button question it tries to validate all other fields on the form and, if any field fails, the server-side script is not run, so the viewScopes are not updated. This is the part that stops Option 4 working on 8.5.

Finally we have a script block (only rendered if we have a viewScope variable called NoteIDError) that adds the error tooltip to the div whose id is NoteID.

The only differences for the checkBox and listBox are in the onClick event, because for those two types a click could be removing the final value.

When the ‘Submit Answers’ button is clicked the validateSpecial() function in option4Validation.js is triggered. This gets the viewScopes, creates an iterator and looks for any viewScopes beginning “NT” that have a value (remember, if the question has been answered, we’re setting the viewScope to “”), and put them in a tilde-separated string. Only after iterating through all viewScopes do we add any viewScope variables for NoteIDError – otherwise our iterator will run through those as well – and set the function to return false.

var viewScopes = viewScope.keySet();

var x = “”;

var it:Iterator=viewScopes.iterator();

var formSubmit = true;

//Loop through and add to variable if validation fails

for (it; it.hasNext();) {

y = it.next();

if (@Left(y, 2) == “NT”) {

if (viewScope.get(y) != “”) x += y + “~”; //if we add the viewScope here, it messes things up

}

}

// Now loop through the variable, set the viewScope variables, and set formSubmit to false

while (x != “”) {

viewScope.put(@Left(x, “~”) + “Error”, “Yes”);

x = @Right(x, “~”);

formSubmit = false;

}

return formSubmit;

This works on 8.5.1, as you can see from the final database which can be downloaded from this link. Hopefully even if you don’t use my final solution, some of the other learning points encountered on the way will be of benefit. It’s not ideal that it runs the client-side validation first, so only shows errors on radioButton Groups, checkBox Groups and ListBoxes after all other fields have been successfully validated. But that is something for another day!

3 thoughts on “XPages: Validation for Radio Buttons, Check Boxes and List Boxes: Part Six”

  1. Paul, good stuff! But wwas there ever a Part Three in this series? … So far, I’ve found parts 1,2,4,5,6.

    Cheers, Tony.

  2. BTW, far be it for a mere colonial to pick up on a matter of usage of the great English language … but in the first sentence it should be “principle” — as in the “Do no harm principle” … See { Link } (Cheers again, Tony).

  3. @Tony

    Thanks for the comments, you’re right on principle, I was rushing before going on holiday! I will amend it.

    Part three was a post asking for help on why part 2 wasn’t working under 8.5.1, the link is here: { Link }

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.