当表单验证期间值为空时也显示错误消息

Also show error message when value is empty during form validation

我遇到的问题实际上什至可以在此处的官方 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);

现在这个表达式不匹配空字符串。然而,我在验证时没有收到任何错误。

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

您可以直接使用setError方法。然后 return 表单返回。

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

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