Implement a Validator

I want to validate a passField but in a runtime and depens on some boolean arguments (capital, digits and alfa).
I decided to implements a Validator (just override apply method) but i found a problem with search a ValidationResut implementation so i decided to use the AbstracValidator.

    @Override
    public ValidationResult apply(String value, ValueContext context) {
        ValidationResult result = this.toResult("", true);
        if(capital) {
            result = new RegexpValidator("al menos una mayúscula", "\\w*[A-Z]
\\w*")
                       .apply(value, context);
        }
        if(!result.isError() && digit) {
            result = new RegexpValidator("al menos un dígito", "\\w*\\d\\w*")
                       .apply(value, context);
        }
        if(!result.isError() && alfa) {
            result = new RegexpValidator("al menos un carácter no alfanumérico", "\\w*[^a-zA-Z0-9]
\\w*")
                       .apply(value, context);
        }
        return result;
    }

is there another way to obtain a ValidationResult without using toResult method of AbstractValidator?
Is there another better implementation of that to validate passField?

Check the chapter “Implementing Custom Validators”, here


https://vaadin.com/docs/v8/framework/components/components-fields.html

Thanks Tatu

OK, now i prefer to use a clase to implements Validator and use Pattern insted of RegexpValidator

@Override
    public ValidationResult apply(String value, ValueContext context) {
        if(capital) {
            if(! Pattern.compile("\\w*[A-Z]
\\w*").matcher(value).matches()) {
                return ValidationResult.error("al menos una mayúscula");
            }    
        }
        if(digit) {
            if(! Pattern.compile("\\w*\\d\\w*").matcher(value).matches()) {
                return ValidationResult.error("al menos un dígito");
            }    
        }
        if(alfa) {
            if(! Pattern.compile("\\w*[^a-zA-Z0-9]
\\w*").matcher(value).matches()) {
                return ValidationResult.error("al menos un caracter no alfanumérico");
            }    
        }
        return ValidationResult.ok();
    }