2

我有一个下拉框和一个 TextField。文本字段的值必须以唯一编号开头,具体取决于选择。如果不是,将显示错误消息。

根据选择,错误消息如下所示:

对于 selection1,“TextField”中的数字必须以 101 开头

或者

“TextField”中的数字必须以 102 开头,用于 selection2

等等

我编写了一个自定义验证器来验证 TextField。

我不喜欢的是,对于每个错误,我都有一个资源键及其对应的错误消息值:

@Override
protected void onValidate(IValidatable<String> validatable) {

    if(selection.equals(selections1) && !beginsWithGoodNumber){
        error(validatable, "error.selection1");
    }else if(selection.equals(selections1) && !beginsWithGoodNumber){
        error(validatable, "error.selection2");
    }else if(selection.equals(selections1) && !beginsWithGoodNumber){
        error(validatable, "error.selection3");
    }
}

在我的属性文件中:

error.selection1 = the number in the '${label}' must begin with 101 for selection1
error.selection2 = the number in the '${label}' must begin with 102 for selection2
error.selection3 = the number in the '${label}' must begin with 103 for selection3
error.selection4 = the number in the '${label}' must begin with 104 for selection4

我想在属性文件中有这样的东西:

error.selection = the number in the '${label}' must begin with {number} for {selection}

其中{number}{selection}是变量。

这可能吗?

谢谢。

4

2 回答 2

1

假设您的验证器扩展了 AbstractValidator,您可以将变量映射传递给#error()。

或者参见 AbstractRangeValidator 了解如何创建 ValidationError、在其上设置变量并将其报告给组件。

于 2013-06-12T07:51:30.220 回答
0

您可能可以设置一个 ValidationError 对象并将其设置在 validatetable.error 方法上。ValidationError 有一个设置变量的方法。

于 2013-06-11T17:12:08.293 回答