Reputation: 2048
I have setup a validator for an inputfield:
<xp:inputTextarea id="inpRelPresentation"
value="#{matterBean.matter.busRelations}"
validator="#{matterValidators.valBusinessRelation}">
<xp:this.validators>
<xp:validateRequired>
<xp:this.message><![CDATA[#{javascript:xptI18NBean.getValue("matter.msg_valid_business_relation")}]]></xp:this.message>
</xp:validateRequired>
</xp:this.validators>
<xp:this.required><![CDATA[#{javascript:return ( submittedBy('btnSendToCommitee'))}]]></xp:this.required>
</xp:inputTextarea>
The required property is checked when I click a button with ID btnSendToCommittee.
When I click other buttons like Save the validator is called.
public void valBusinessRelation(FacesContext context, UIComponent component, Object value) throws ValidatorException {
System.out.println("start valBusinessRelation");
if (value == null || StringUtil.isEmpty((String) value)) {
System.out.println("do a return");
return;
}
String trimmed = ((String) value).trim();
if (trimmed.toString().replaceAll("\\s+","").equals("")){
System.out.println("empty after trimming etc, throw validator exception");
String msgTxt = langBean.getValue("matter.msg_valid_business_relation");
FacesMessage msg = new FacesMessage(msgTxt, msgTxt);
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
I would also like to add the condition that this validator is called ONLY when the btnSendToCommittee is pressed. I have no idea how to do this because the submittedBy('btnSendToCommitee') check is ssjs and call to the valBusinessRelation() method is EL.
I did not find any complete examples how to call a validator by validator-id described here: http://hasselba.ch/blog/?p=764
Upvotes: 1
Views: 66
Reputation: 868
I've added java code needed to do submittedBy-check in Java in my original blogpost: http://dontpanic82.blogspot.com/2010/03/xpages-making-validation-behave.html
@SuppressWarnings( "unchecked" )
public static String getParameter( String name ) {
Map<String, String> parameters = resolveVariable( "param", Map.class );
return parameters.get( name );
}
@SuppressWarnings( "unchecked" )
public static UIComponent getChildComponent( UIComponent component, String id ) {
if( id.equals( component.getId() ) ) {
return component;
}
Iterator<UIComponent> children = component.getFacetsAndChildren();
while( children.hasNext() ) {
UIComponent child = children.next();
UIComponent found = getChildComponent( child, id );
if( found != null ) {
return found;
}
}
return null;
}
public static <T> T resolveVariable( String name, Class<T> typeClass ) {
Object variable = ExtLibUtil.resolveVariable( FacesContext.getCurrentInstance(), name );
return variable == null ? null : typeClass.cast( variable );
}
public static UIComponent getComponent( String id ) {
return getChildComponent( (UIViewRootEx) FacesContext.getCurrentInstance().getViewRoot(), id );
}
public static boolean wasSubmittedByComponentId( String componentId ) {
if( componentId == null ) {
return false;
}
String eventHandlerClientId = getParameter( "$$xspsubmitid" );
if( eventHandlerClientId == null ) {
return false;
}
// Extract the component id for the event handler
String eventHandlerComponentId = eventHandlerClientId.replaceFirst( "^.*\\:(.*)$", "$1" );
UIComponent eventHandler = getComponent( eventHandlerComponentId );
if( eventHandler == null ) {
return false;
}
// Fetch the component the event handler belongs to
UIComponent submissionComponent = eventHandler.getParent();
if( submissionComponent == null ) {
return false;
}
return ( componentId.equals( submissionComponent.getId() ) );
}
The code was copy/pasted from different Java classes. Let me know if there is anything missing :)
Upvotes: 1