Search in sources :

Example 1 with ConstraintException

use of grails.validation.exceptions.ConstraintException in project grails-core by grails.

the class ConstrainedProperty method applyConstraint.

/**
     * Applies a constraint for the specified name and consraint value.
     *
     * @param constraintName The name of the constraint
     * @param constrainingValue The constraining value
     *
     * @throws ConstraintException Thrown when the specified constraint is not supported by this ConstrainedProperty. Use <code>supportsContraint(String constraintName)</code> to check before calling
     */
@Override
public void applyConstraint(String constraintName, Object constrainingValue) {
    if (constraints.containsKey(constraintName)) {
        if (constrainingValue == null) {
            appliedConstraints.remove(constraintName);
        } else {
            try {
                Constraint c = instantiateConstraint(constraintName, true);
                if (c != null) {
                    c.setParameter(constrainingValue);
                    appliedConstraints.put(constraintName, c);
                }
            } catch (Exception e) {
                LOG.error("Exception thrown applying constraint [" + constraintName + "] to class [" + owningClass + "] for value [" + constrainingValue + "]: " + e.getMessage(), e);
                throw new ConstraintException("Exception thrown applying constraint [" + constraintName + "] to class [" + owningClass + "] for value [" + constrainingValue + "]: " + e.getMessage(), e);
            }
        }
    } else if (bean.isWritableProperty(constraintName)) {
        bean.setPropertyValue(constraintName, constrainingValue);
    } else {
        throw new ConstraintException("Constraint [" + constraintName + "] is not supported for property [" + propertyName + "] of class [" + owningClass + "] with type [" + propertyType + "]");
    }
}
Also used : 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) ConstraintException(grails.validation.exceptions.ConstraintException) MissingPropertyException(groovy.lang.MissingPropertyException) ConstraintException(grails.validation.exceptions.ConstraintException)

Example 2 with ConstraintException

use of grails.validation.exceptions.ConstraintException in project grails-core by grails.

the class ConstrainedProperty method setUrl.

/**
     * @param url The url to set.
     */
public void setUrl(boolean url) {
    if (!isValidStringType()) {
        throw new ConstraintException("Url constraint can only be applied to String properties");
    }
    Constraint c = appliedConstraints.get(URL_CONSTRAINT);
    if (url) {
        if (c == null) {
            c = new UrlConstraint();
            c.setOwningClass(owningClass);
            c.setPropertyName(propertyName);
            appliedConstraints.put(URL_CONSTRAINT, c);
        }
        c.setParameter(true);
    } else {
        if (c != null) {
            appliedConstraints.remove(URL_CONSTRAINT);
        }
    }
}
Also used : 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) UrlConstraint(org.grails.validation.UrlConstraint) ConstraintException(grails.validation.exceptions.ConstraintException)

Example 3 with ConstraintException

use of grails.validation.exceptions.ConstraintException in project grails-core by grails.

the class ConstrainedProperty method setMatches.

/**
     * @param regex The matches to set.
     */
public void setMatches(String regex) {
    if (!isValidStringType()) {
        throw new ConstraintException("Matches constraint can only be applied to String properties");
    }
    Constraint c = appliedConstraints.get(MATCHES_CONSTRAINT);
    if (regex == null) {
        appliedConstraints.remove(MATCHES_CONSTRAINT);
    } else {
        if (c == null) {
            c = new MatchesConstraint();
            c.setOwningClass(owningClass);
            c.setPropertyName(propertyName);
            appliedConstraints.put(MATCHES_CONSTRAINT, c);
        }
        c.setParameter(regex);
    }
}
Also used : MatchesConstraint(org.grails.validation.MatchesConstraint) 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) ConstraintException(grails.validation.exceptions.ConstraintException)

Example 4 with ConstraintException

use of grails.validation.exceptions.ConstraintException in project grails-core by grails.

the class ConstrainedProperty method setCreditCard.

/**
     * @param creditCard The creditCard to set.
     */
public void setCreditCard(boolean creditCard) {
    if (!isValidStringType()) {
        throw new ConstraintException("CreditCard constraint can only be applied to String properties");
    }
    Constraint c = appliedConstraints.get(CREDIT_CARD_CONSTRAINT);
    if (creditCard) {
        if (c == null) {
            c = new CreditCardConstraint();
            c.setOwningClass(owningClass);
            c.setPropertyName(propertyName);
            appliedConstraints.put(CREDIT_CARD_CONSTRAINT, c);
        }
        c.setParameter(true);
    } else {
        if (c != null) {
            appliedConstraints.remove(CREDIT_CARD_CONSTRAINT);
        }
    }
}
Also used : 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) CreditCardConstraint(org.grails.validation.CreditCardConstraint) ConstraintException(grails.validation.exceptions.ConstraintException)

Example 5 with ConstraintException

use of grails.validation.exceptions.ConstraintException in project grails-core by grails.

the class ConstrainedProperty method setBlank.

/**
     * @param blank The blank to set.
     */
public void setBlank(boolean blank) {
    if (!isValidStringType()) {
        throw new ConstraintException("Blank constraint can only be applied to String properties");
    }
    if (!blank) {
        Constraint c = appliedConstraints.get(BLANK_CONSTRAINT);
        if (c == null) {
            c = new BlankConstraint();
            c.setOwningClass(owningClass);
            c.setPropertyName(propertyName);
            appliedConstraints.put(BLANK_CONSTRAINT, c);
        }
        c.setParameter(blank);
    } else {
        appliedConstraints.remove(BLANK_CONSTRAINT);
    }
}
Also used : 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) BlankConstraint(org.grails.validation.BlankConstraint) ConstraintException(grails.validation.exceptions.ConstraintException)

Aggregations

ConstraintException (grails.validation.exceptions.ConstraintException)6 BlankConstraint (org.grails.validation.BlankConstraint)6 CreditCardConstraint (org.grails.validation.CreditCardConstraint)6 EmailConstraint (org.grails.validation.EmailConstraint)6 InListConstraint (org.grails.validation.InListConstraint)6 MatchesConstraint (org.grails.validation.MatchesConstraint)6 MaxConstraint (org.grails.validation.MaxConstraint)6 MaxSizeConstraint (org.grails.validation.MaxSizeConstraint)6 MinConstraint (org.grails.validation.MinConstraint)6 MinSizeConstraint (org.grails.validation.MinSizeConstraint)6 NotEqualConstraint (org.grails.validation.NotEqualConstraint)6 NullableConstraint (org.grails.validation.NullableConstraint)6 RangeConstraint (org.grails.validation.RangeConstraint)6 ScaleConstraint (org.grails.validation.ScaleConstraint)6 SizeConstraint (org.grails.validation.SizeConstraint)6 UrlConstraint (org.grails.validation.UrlConstraint)6 ValidatorConstraint (org.grails.validation.ValidatorConstraint)6 MissingPropertyException (groovy.lang.MissingPropertyException)1