如何使用XPages Java代码在自定义控件中设置输入控件的有效方法? [英] How to use XPages Java code to set valid method of input control inside a custom control?

查看:52
本文介绍了如何使用XPages Java代码在自定义控件中设置输入控件的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这很奇怪.

在XPages中,我的dataSource是一个Java对象.托管bean或PageController.我正在通过Ext使用引导程序.图书馆.

In XPages my dataSource is a Java Object. A Managed bean or PageController. I'm using bootstrap via the Ext. Library.

我想做的是将所有验证代码保存在Java对象中,而不是将任何附加内容附加到XPage上的控件中.在java对象中,我可以通过以下方式添加任何错误消息:FacesContext.getCurrentInstance().addMessage

What I'd like to do is keep all my validation code inside my Java Object rather then attaching anything to the control on the XPage. Inside the java object I can add any error messages via: FacesContext.getCurrentInstance().addMessage

因此,可以通过控件显示任何错误.

So any errors can by showed via an control.

如果我想向特定(单个)控件发送消息,我不知道该怎么做是针对单个控件.

BUT what I don't know how to do is to target an individual control if I wanted to send a message to a specific (singular) control.

实际上,我希望能够使用此示例进行引导字段自定义控件: http://www.bootstrap4xpages.com/bs4xp/demos.nsf/reusableFields.xsp

Realistically what I'd love to be able to is use this example for a bootstrap field custom control : http://www.bootstrap4xpages.com/bs4xp/demos.nsf/reusableFields.xsp

并从我的Java类中设置了isValid方法,以便通过has-error样式将bootstrap字段显示为红色.

and from my Java class set the isValid method so the bootstrap field is rendered red via the has-error style.

任何关于如何进入自定义控件内的输入控件的isValid的建议,甚至是让我通过java进行验证但控制字段样式的另一种方法,都将受到赞赏.

Any advice on how to get to the the isValid for an input control inside a custom control or even an alternative method to let me validate via java but control the field styling would be appreciated.

谢谢

推荐答案

我正在为组件使用自定义验证器.例如,

What I do is using a custom validator for the component. For instance,

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

在我的控制器课程中:

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

BeanUtils方法使特定组件无效并生成验证错误消息:

BeanUtils methods to invalidate a specific component and generate a validation error message:

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);
}

这篇关于如何使用XPages Java代码在自定义控件中设置输入控件的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆