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 + "]");
}
}
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);
}
}
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations