Possible smallish bug with validation

As per one of the answers (How to translate validation messages - CUBA.Platform), I implemented a validator programatically for a Field based on com.haulmont.cuba.gui.components.Field.Validator. The field was required.

If I don’t specify a required message for when it is empty, the actual message for when it fails the validator doesn’t show up.


usernameField.addValidator(new Field.Validator() {
    private final String EMAIL_REGEX =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private final Pattern PATTERN = Pattern.compile(EMAIL_REGEX);

    @Override
    public void validate(Object value) throws ValidationException {
        String email = value.toString();
        if ((value == null) || "".equals(email) || !PATTERN.matcher(email).matches()) {
            throw new ValidationException("The username must be a valid email address");
        }
    }
});

For example, what I’m finding is that if you don’t specify a required message like:


passwordField.setRequiredMessage("The password cannot be empty");

The alert notification comes up empty when it fails the valid email test. If you do set a required message, then the “The username must be a valid email address” message comes up properly when the field is non-empty but doesn’t contain a valid email.

BTW, is this the place to post bug reports or should I post them somewhere else?

Hi Jonathan,

The behaviour you have described is current normal process of validation:

  • Validators are invoked only for non-empty fields
  • If a field is required and empty RequiredValueMissingException is thrown with requiredMessage assigned to the field
  • If a field is required and non-empty your Validator will be triggerred and can throw ValidationException

If you connect your field to a datasource then requiredMessage can be assigned automatically according to your data model.

And please post any bug reports here on forum using Report a problem topic type.

new-topic