Skip to content

Instantly share code, notes, and snippets.

@siosio
Created July 7, 2012 04:47
Show Gist options
  • Save siosio/3064750 to your computer and use it in GitHub Desktop.
Save siosio/3064750 to your computer and use it in GitHub Desktop.
package siosio.validator
import org.codehaus.groovy.grails.validation.AbstractConstraint
import org.springframework.validation.Errors
class NumCharValidator extends AbstractConstraint {
public static final NAME = "number"
private static final String DEFAULT_MESSAGE = "default.invalid.number.message"
private boolean number = false
@Override
void setParameter(Object constraintParameter) {
if (constraintParameter instanceof Boolean) {
number = constraintParameter
}
super.setParameter(constraintParameter)
}
@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
if (number) {
if (!(propertyValue as String ==~ /[0-9]+/)) {
super.rejectValue(target, errors, DEFAULT_MESSAGE,
NAME, constraintPropertyName, constraintOwningClass)
}
}
}
@Override
protected boolean skipBlankValues() {
true
}
boolean supports(Class type) {
String.class.isAssignableFrom(type)
}
String getName() {
NAME
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment