首页 > 解决方案 > 在表单验证期间值为空时也会显示错误消息

问题描述

我遇到的问题实际上甚至可以在此处的官方 SmartGWT 演示中看到:https ://www.smartclient.com/smartgwt/showcase/#form_validation_regexp

如果您不输入任何内容(将字段留空)并按验证,则不会显示任何错误。对于所需的值,即使该字段为空,我也需要显示错误。

我将我的验证器设置为:

    RegExpValidator regExpValidator = new RegExpValidator();  
    regExpValidator.setExpression("^[0-9A-Z_]{7,12}$");  
    regExpValidator.setErrorMessage("Code must contain capital letters and numbers");
    codeField.setValidators(regExpValidator);

现在这个表达式不匹配一个空字符串。但是,我在验证时没有错误。

如何在表单中显示空所需值的错误?

标签: javagwtsmartgwt

解决方案


您可以直接使用 setError 方法。并返回表格。

if(codeField.getText().trim().isEmpty()){
codeField.setError("The Code must not be Empty.");
return;
}

这将返回表单并验证空字符串。


推荐阅读