![](/img/trans.png)
[英]How can I check if a textbox only contains certain characters when a button is pressed
[英]Validator for XPages textarea control, only when a certain button is pressed
我已经为输入字段设置了一个验证器:
<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>
单击 ID 为 btnSendToCommittee 的按钮时,将检查所需的属性。
当我单击其他按钮(如保存)时,将调用验证器。
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);
}
}
我还想添加一个条件,即仅当按下 btnSendToCommittee 时才调用此验证器。 我不知道如何执行此操作,因为 submittedBy('btnSendToCommitee') 检查是 ssjs 并且对 valBusinessRelation() 方法的调用是 EL。
我没有找到任何完整的示例如何通过此处描述的 validator-id 调用验证器: http://hasselba.ch/blog/?p=764
我在我的原始博文中添加了 java 代码以执行提交检查 Java: 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() ) );
}
代码是从不同的 Java 类复制/粘贴的。 让我知道是否缺少任何东西:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.