Skip to content

Instantly share code, notes, and snippets.

@dstarh
Created April 15, 2011 15:54
Show Gist options
  • Save dstarh/921930 to your computer and use it in GitHub Desktop.
Save dstarh/921930 to your computer and use it in GitHub Desktop.
warning you're going to be dumber after reading this
package util.validation;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import play.data.validation.Check;
public class RequiredIfOtherEqualsValueMin extends Check {
private static final String VALIDATION_MIN = "validation.min";
private static final String VALIDATION_REQUIRED = "validation.required";
@Override
public boolean isSatisfied(Object validatedObject, Object value) {
String msg = checkWithCheck.getMessage();
if (!msg.contains(",")) {
throw new IllegalArgumentException("should be in format of property,value,length");
}
String[] values = ((String) msg).split(",");
Integer min = Integer.parseInt(values[2]);
try {
String propertyValue = BeanUtils.getSimpleProperty(validatedObject, values[0]);
boolean objIsBlankOrNull = value instanceof String ? StringUtils.isBlank((String) value) : value == null;
if (propertyValue != null && propertyValue.equals(values[1])) {
if (objIsBlankOrNull) {
setMessage(VALIDATION_REQUIRED);
return false;
}
else {
if (((String) value).length() < min) {
setMessage(VALIDATION_MIN, values[2]);
return false;
}
else {
return true;
}
}
}
else {
return true;
}
}
catch (IllegalAccessException e) {
}
catch (InvocationTargetException e) {
}
catch (NoSuchMethodException e) {
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment