How to use XPages Java code to set a valid input control method inside a custom control?

OK, this is weird.

In XPages, my dataSource is a Java object. Managed bean or PageController. I am using bootstrap through Ext. Library.

What I would like to do is to save all my validation code inside my Java object, and not attach anything to the XPage control. Inside the java object, I can add any error messages via: FacesContext.getCurrentInstance (). Addmessage

Thus, any errors can be displayed using the control.

BUT that I do not know how to do this is to configure an individual control if I want to send a message to a specific (single) control.

Actually, I would like to use this example for a custom element of the bootstrap field: http://www.bootstrap4xpages.com/bs4xp/demos.nsf/reusableFields.xsp

and from my Java class, set the isValid method, so the bootstrap field is displayed in red using the has-error style.

Any advice on how to get to the isValid value for an input control inside a custom control or even an alternative method that allows me to check through java but control the style of the field will be appreciated.

thank

+4
source share
2 answers

I am using a custom validator for the component. For instance,

<xp:this.validators>
    <xp:customValidator validate="#{javascript:controller.validateDuration(this)}"></xp:customValidator>
</xp:this.validators>

In my controller class:

public void validateDuration(UIComponent source) {
    // Do my checks...
    // If fails,
    BeanUtils.setControlInvalid(source, "Format Error at date time!");
}

BeanUtils :

public static void setControlInvalid(UIComponent editableComponent, String message) {
    if(StringUtil.isEmpty(message) || editableComponent==null) return;

    if(editableComponent instanceof EditableValueHolder) {
        ((EditableValueHolder) editableComponent).setValid(false);
        postFacesMessage(editableComponent, FacesMessage.SEVERITY_ERROR, message);
    }

}

public static void postFacesMessage(UIComponent component, Severity severity, String msg) {
    if(StringUtil.isEmpty(msg) || component==null) return;

    FacesContext fc=FacesContext.getCurrentInstance();

    FacesMessage message=new FacesMessage(severity, msg, msg);
    fc.addMessage(component.getClientId(fc), message);
}
+2

UIComponent setValid(boolean). binding, Java, . bean.

, , , bean.

, , . bean Map<String, Component> myCunningBindings, , :

<this.loaded><![CDATA[${javascript:myBean.getMyCunningBindings().put(compositeData.fieldMapping, this);
return "";}]]>
</xp:this.loaded>

, . ( ) . , , , compositeData. , true.

bean, , , , , , , . setValid (false).

+1

Source: https://habr.com/ru/post/1584481/


All Articles