javagwtsmartgwt

Also show error message when value is empty during form validation


The problem I have is actually even visible in the official SmartGWT demo here: https://www.smartclient.com/smartgwt/showcase/#form_validation_regexp

If you enter nothing (leave field empty) and press validate, no error is displayed. For required value, I need the error shown even when the field is empty.

I set my validator to this:

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

Now this expression does NOT match an empty string. Yet, I get no error on validation.

How to show errors for empty required values in forms?


Solution

  • You can directly use setError method. And return the form back.

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

    This will give the form back and also validate the empty string.