All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.jqm4gwt.form.validators.CompareStringValueValidator Maven / Gradle / Ivy

package com.sksamuel.jqm4gwt.form.validators;

import com.google.gwt.user.client.ui.HasValue;

public abstract class CompareStringValueValidator implements Validator {

    private final HasValue  hasValue1;
    private final HasValue  hasValue2;
    private final String            msg;

    public CompareStringValueValidator(HasValue hasValue1, HasValue hasValue2,
                                       String validationMsg) {
        this.hasValue1 = hasValue1;
        this.hasValue2 = hasValue2;
        this.msg = validationMsg;
    }

    /**
     * @param val1 - already trim()
     * @param val2 - already trim()
     * @return - true if validation successful, i.e. no validation message should be shown.
     */
    protected abstract boolean areValuesValid(String val1, String val2);

    @Override
    public String validate() {
        String value1 = hasValue1.getValue();
        String value2 = hasValue2.getValue();
        if (value1 == null) return null;
        if (value2 == null) return null;
        value1 = value1.trim();
        if (value1.length() == 0) return null;
        value2 = value2.trim();
        if (value2.length() == 0) return null;
        if (areValuesValid(value1, value2)) return null;
        return msg;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy