Search in sources :

Example 1 with RangeConstraint

use of org.grails.validation.RangeConstraint in project grails-core by grails.

the class ConstrainedProperty method setRange.

/**
     * @param range The range to set.
     */
@SuppressWarnings("rawtypes")
public void setRange(Range range) {
    if (appliedConstraints.containsKey(MAX_CONSTRAINT)) {
        LOG.warn("Setting range constraint on property [" + propertyName + "] of class [" + owningClass + "] forced removal of max constraint");
        appliedConstraints.remove(MAX_CONSTRAINT);
    }
    if (appliedConstraints.containsKey(MIN_CONSTRAINT)) {
        LOG.warn("Setting range constraint on property [" + propertyName + "] of class [" + owningClass + "] forced removal of min constraint");
        appliedConstraints.remove(MIN_CONSTRAINT);
    }
    if (range == null) {
        appliedConstraints.remove(RANGE_CONSTRAINT);
    } else {
        Constraint c = appliedConstraints.get(RANGE_CONSTRAINT);
        if (c == null) {
            c = new RangeConstraint();
            c.setOwningClass(owningClass);
            c.setPropertyName(propertyName);
            appliedConstraints.put(RANGE_CONSTRAINT, c);
        }
        c.setParameter(range);
    }
}
Also used : RangeConstraint(org.grails.validation.RangeConstraint) MaxSizeConstraint(org.grails.validation.MaxSizeConstraint) MinConstraint(org.grails.validation.MinConstraint) SizeConstraint(org.grails.validation.SizeConstraint) MinSizeConstraint(org.grails.validation.MinSizeConstraint) BlankConstraint(org.grails.validation.BlankConstraint) RangeConstraint(org.grails.validation.RangeConstraint) ValidatorConstraint(org.grails.validation.ValidatorConstraint) ScaleConstraint(org.grails.validation.ScaleConstraint) NullableConstraint(org.grails.validation.NullableConstraint) MaxConstraint(org.grails.validation.MaxConstraint) CreditCardConstraint(org.grails.validation.CreditCardConstraint) UrlConstraint(org.grails.validation.UrlConstraint) InListConstraint(org.grails.validation.InListConstraint) MatchesConstraint(org.grails.validation.MatchesConstraint) NotEqualConstraint(org.grails.validation.NotEqualConstraint) EmailConstraint(org.grails.validation.EmailConstraint)

Example 2 with RangeConstraint

use of org.grails.validation.RangeConstraint in project grails-core by grails.

the class ConstrainedProperty method getMax.

/**
     * @return Returns the max.
     */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Comparable getMax() {
    Comparable maxValue = null;
    MaxConstraint maxConstraint = (MaxConstraint) appliedConstraints.get(MAX_CONSTRAINT);
    RangeConstraint rangeConstraint = (RangeConstraint) appliedConstraints.get(RANGE_CONSTRAINT);
    if (maxConstraint != null || rangeConstraint != null) {
        Comparable maxConstraintValue = maxConstraint == null ? null : maxConstraint.getMaxValue();
        Comparable rangeConstraintHighValue = rangeConstraint == null ? null : rangeConstraint.getRange().getTo();
        if (maxConstraintValue != null && rangeConstraintHighValue != null) {
            maxValue = (maxConstraintValue.compareTo(rangeConstraintHighValue) < 0) ? maxConstraintValue : rangeConstraintHighValue;
        } else if (maxConstraintValue == null && rangeConstraintHighValue != null) {
            maxValue = rangeConstraintHighValue;
        } else if (maxConstraintValue != null && rangeConstraintHighValue == null) {
            maxValue = maxConstraintValue;
        }
    }
    return maxValue;
}
Also used : RangeConstraint(org.grails.validation.RangeConstraint) MaxConstraint(org.grails.validation.MaxConstraint)

Example 3 with RangeConstraint

use of org.grails.validation.RangeConstraint in project grails-core by grails.

the class ConstrainedProperty method getMin.

/**
     * @return Returns the min.
     */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Comparable getMin() {
    Comparable minValue = null;
    MinConstraint minConstraint = (MinConstraint) appliedConstraints.get(MIN_CONSTRAINT);
    RangeConstraint rangeConstraint = (RangeConstraint) appliedConstraints.get(RANGE_CONSTRAINT);
    if (minConstraint != null || rangeConstraint != null) {
        Comparable minConstraintValue = minConstraint != null ? minConstraint.getMinValue() : null;
        Comparable rangeConstraintLowValue = rangeConstraint != null ? rangeConstraint.getRange().getFrom() : null;
        if (minConstraintValue != null && rangeConstraintLowValue != null) {
            minValue = (minConstraintValue.compareTo(rangeConstraintLowValue) > 0) ? minConstraintValue : rangeConstraintLowValue;
        } else if (minConstraintValue == null && rangeConstraintLowValue != null) {
            minValue = rangeConstraintLowValue;
        } else if (minConstraintValue != null && rangeConstraintLowValue == null) {
            minValue = minConstraintValue;
        }
    }
    return minValue;
}
Also used : RangeConstraint(org.grails.validation.RangeConstraint) MinConstraint(org.grails.validation.MinConstraint)

Aggregations

RangeConstraint (org.grails.validation.RangeConstraint)3 MaxConstraint (org.grails.validation.MaxConstraint)2 MinConstraint (org.grails.validation.MinConstraint)2 BlankConstraint (org.grails.validation.BlankConstraint)1 CreditCardConstraint (org.grails.validation.CreditCardConstraint)1 EmailConstraint (org.grails.validation.EmailConstraint)1 InListConstraint (org.grails.validation.InListConstraint)1 MatchesConstraint (org.grails.validation.MatchesConstraint)1 MaxSizeConstraint (org.grails.validation.MaxSizeConstraint)1 MinSizeConstraint (org.grails.validation.MinSizeConstraint)1 NotEqualConstraint (org.grails.validation.NotEqualConstraint)1 NullableConstraint (org.grails.validation.NullableConstraint)1 ScaleConstraint (org.grails.validation.ScaleConstraint)1 SizeConstraint (org.grails.validation.SizeConstraint)1 UrlConstraint (org.grails.validation.UrlConstraint)1 ValidatorConstraint (org.grails.validation.ValidatorConstraint)1