Skip to main content link. Accesskey S

The useful resource for IBM Lotus Domino XPages development

Submit Search

Available in the Appstore!All the content of xpageswiki.com for iPhone or iPad for offline access.

Home > Working with fields > Work with validation and message controls

Work with validation and message controls

ShowTable of Contents

Use your own validation tests


Create a Expression Validator and use JavaScript for comparing stuff. The value of the fields is stored in the variable "value".

value == sessionScope.get("comparevalue");


Here the validator is true if the value of the fields is the same than the value of "comparevalue" in the sessionScope.

Note: if you have a radio button group, you need the validator only in the first field of the group.

Useful regular expressions for the constraint validator

Email address


b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,7}b


Thanks, Ross S. Swick

Check if one date is greate as the other


var start = value;
var end = getComponent("D_END").getValue();
if (!start) return false;
if (!end) return false;
var dStart:NotesDateTime = session.createDateTime(start);
var dEnd:NotesDateTime = session.createDateTime(end);

return (dEnd.timeDifference(dStart) > 0 )

Check if a value is a mail address


Use a constraints validator with this regex:

^s*[w-+_]+(.[w-+_]+)*@[w-+_]+.[w-+_]+(.[w-+_]+)*s*$

Run validation only on a button click


Standard validators are running on each (full / partial) update, which can be very annoying.
IBM is aware of that problem in 851 and is working on a general solution.


In the meantime, Tommy Valand developed a good workaround.
Click here to see Tommy's solution

Update: as of Domino 8.5.2, validators can be enabled and disabled with SSJS.

Display a custom text on a message control


Normally, a message control is bound to a field to display the error messages of it's validators.
Tommy Valand found a solution for sending custom text to a message control, so that you can display all kind of information in such a control:

function addFacesMessage(message, component){
try { 
if(typeof component === 'string' ){
component = getComponent(component);
}

var clientId = null;
if (component) {
clientId = component.getClientId(facesContext);
}

facesContext.addMessage(clientId, 
new javax.faces.application.FacesMessage(message));
} catch(e){ }
}


The second parameter is the component id of the field (!) to which a message control is bound, or the field control itself.
If the parameter 'component' is null or missing, then the message is displayed in the global scope, that means in app xp:messages (note the "s") controls.

Thanks to Tommy Valand

Displaying validation messages for multiple controls in one message component


Create a xp:message control like this:

<xp:text styleClass="xspMessage" 
escape="false" rendered="#{javascript:return ( this.value );}">
<xp:this.value>
<![CDATA[#{javascript:return getFacesMessages( [ 'field1', 'field2' ] ).join( '<br />' );}]]>
</xp:this.value>
</xp:text>


Note that this message controls only renderes if it has something to display.

Then create a SJSS function like this:

function getFacesMessages( components ){
try { 
// ensure the parameter is an array
if (typeof components !== 'array') { components = [components]; } 
var clientId, component, messages = [], msgIterator; 
for (var i = 0; i < components.length; i++ ) {
component = components[i];
if (typeof component === 'string') {
clientId = getClientId(component);
} else {
clientId = component.getClientId(facesContext);
}

msgIterator = facesContext.getMessages(clientId);
if (!msgIterator) {continue;} 
while (msgIterator.hasNext()) {
messages.push(msgIterator.next().getSummary());
}
}
return messages; 
} catch( e ){ }
}


Another goodie found by Tommy Valand

Validating a Read Only field


By default a field set to be read only cannot be required, but with a little help from my friend Chris Toohey you can add a div tag with a style to make it work like shown below:




<div style="display: none;"> <xp:inputText id="rptToName1" value="#{xp_record.RptToName}" required="#{javascript:return submittedBy( 'questionairreBtn1' )}" styleClass="tableCellLabelLeftNormal"> <xp:this.validators> <xp:validateRequired message="Who this position reports to is required"></xp:validateRequired> </xp:this.validators>

</xp:inputText> </div>



The field property should have read only deselected and the visible property set to true, then you create a visible computed field that uses the value of the now hidden control which is required and magically it works like a charm. I have also submitted this idea - xPages Required Property - on IdeaJam and the developerworks wiki



Dwain A Wuerfel


Created by Jose Sarotto on Jun 3, 2011 11:17:35 PM

Thanks for your invaluable tips.

Regarding "Check if a value is a mail address"

Instead of using

^s*[w-+_]+(.[w-+_]+)*@[w-+_]+.[w-+_]+(.[w-+_]+)*s*$

I think you need to use the expression as in the following function:

----------------------------------------------------------

function checkEmail(str)

{

var eMail = /^\s*[\w\-\+\_]+(\.[\w\-\+\_]+)*@[\w\-\+\_]+\.[\w\-\+\_]+(\.[\w\-\+\_]+)*\s*$/;

if (str.match(eMail)) {

return true;

} else {

return false;

}

}

-----------------------------------------------------


Created by Peter Leugner on Jun 10, 2011 10:17:14 PM

Another easy way to run a validation only when a certain button/link is clicked is shonw below:

title="Reject this request with a comment">

value="#{Workflow.comment}" style="width:400px" rows="5">

message="Please provide a reason for the rejection in the comment field!">

Just give the eventhandler an id and check for the id in the required attribute of the field


Created by Peter Leugner on Jun 10, 2011 10:18:30 PM

oops, can't post the xml, it get's filtered out :-(


Add Comment

Name:
Comments:
Use  searchlotus.com  for news in the Web related to Lotus Notes and Domino,
and to search those sites.
Check  youatnotes.com  for great Lotus Notes, Domino and XPages software.
Did this information help you?
Please honor our efforts and flattr this!