Created
April 15, 2011 15:54
-
-
Save dstarh/921930 to your computer and use it in GitHub Desktop.
warning you're going to be dumber after reading this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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