public class ExampleChecks implements Serializable {
public ExampleChecks() {
super();
}
public static boolean alwaysBad(
Object bean,
ValidatorAction va,
Field field,
ActionMessages errors,
Validator validator,
HttpServletRequest request) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
}
<validator name="bad" classname="com.snail.ExampleChecks" method="alwaysBad" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.bad"/>
errors.bad={0} is always bad.
<form name="ExampleForm"> <field property="value" depends="bad"> <arg key="label.value"/> </field> </form>
public class ExampleChecks implements Serializable {
public ExampleChecks() {
super();
}
public static boolean alwaysBad(Object bean, ValidatorAction va,
Field field, ActionMessages errors, Validator validator,
HttpServletRequest request) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
public static boolean onlyOkisOk(Object bean, ValidatorAction va,
Field field, ActionMessages errors, Validator validator,
HttpServletRequest request) throw JSPException {
// 検証対象の切り出し
String str = null;
if( bean instanceof String ){
str = (String)bean;
}else{
try {
str = PropertyUtils.getProperty(bean,field.getProperty()).toString();
} catch (Exception e) {
throw new JspException(e);
}
}
if("Ok".equals(str)){
return true;
}else{
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
}
}
<validator name="OkIsOk" classname="com.snail.ExampleChecks" method="onlyOkisOk" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.onlyOkisOk"/>
errors.onlyOkisOk={0} is only Ok is ok.
<form name="ExampleForm"> <field property="value" depends="OkIsOk"> <arg key="label.value"/> </field> </form>
public class ExampleChecks implements Serializable {
public static boolean onlyAnyOneisOk(Object bean, ValidatorAction va,
Field field, ActionMessages errors, Validator validator,
HttpServletRequest request) throws JspException {
// 検証対象の切り出し
String str = null;
if (bean instanceof String) {
str = (String) bean;
} else {
try {
str = PropertyUtils.getProperty(bean, field.getProperty())
.toString();
} catch (Exception e) {
throw new JspException(e);
}
}
// 検証対象がnullか空文字の場合には、検証成功とする
if( str == null || "".equals(str.trim())){
return true;
}
// 候補の読み込み
String candidateList = field.getVarValue("Candidate");
String[] candidates = candidateList.split(",");
for (int cnt = 0; cnt < candidates.length; cnt++) {
if (candidates[cnt].equals(str)) {
return true;
}
}
errors.add(field.getKey(), Resources.getActionMessage(request, va,
field));
return false;
}
}
<validator name="AnyOneIsOk" classname="com.snail.ExampleChecks" method="onlyAnyOneisOk" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.AnyOneIsOk"/>
errors.AnyOneIsOk={0} must be one of {1}.
<form name="ExampleForm"> <field property="value" depends="AnyOneIsOk"> <arg0 key="label.value"/> <arg1 key="${var:Candidate}" resource="false"/> <var> <var-name>Candidate</var-name> <var-value>diamond,perl,ruby,crystal,gold</var-value> </var> </field> </form>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation> <global> <validator name="bad" classname="com.snail.ExampleChecks" method="alwaysBad" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.bad"/> <validator name="OkIsOk" classname="com.snail.ExampleChecks" method="onlyOkisOk" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.onlyOkisOk"/> <validator name="AnyOneIsOk" classname="com.snail.ExampleChecks" method="onlyAnyOneisOk" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" depends="" msg="errors.AnyOneIsOk"/> </global> </form-validation>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml,/WEB-INF/myValidator-rules.xml"/> </plug-in>
□□□□□□□□□□javax.servlet.http.HttpServletRequest"← □□□depends=""← □□□msg="errors.onlyOkisOk"/>← ■■← □□<validator name="AnyOneIsOk"← □□□classname="com.snail.ExampleChecks"← □□□method="onlyAnyOneisOk"← □□□methodParams="java.lang.Object,← □□□□□□□□□□org.apache.commons.validator.ValidatorAction,← □□□□□□□□□□org.apache.commons.validator.Field,← □□□□□□□□□□org.apache.struts.action.ActionMessages,← □□□□□□□□□□org.apache.commons.validator.Validator,← □□□□□□□□□□javax.servlet.http.HttpServletRequest"← □□□depends=""← □□□msg="errors.AnyOneIsOk"/>← ■■← □</global>← (□■:SPC , ←:改行)
□□□□□□□□□□javax.servlet.http.HttpServletRequest"← □□□depends=""← □□□msg="errors.onlyOkisOk"/>← ← □□<validator name="AnyOneIsOk"← □□□classname="com.snail.ExampleChecks"← □□□method="onlyAnyOneisOk"← □□□methodParams="java.lang.Object,← □□□□□□□□□□org.apache.commons.validator.ValidatorAction,← □□□□□□□□□□org.apache.commons.validator.Field,← □□□□□□□□□□org.apache.struts.action.ActionMessages,← □□□□□□□□□□org.apache.commons.validator.Validator,← □□□□□□□□□□javax.servlet.http.HttpServletRequest"← □□□depends=""← □□□msg="errors.AnyOneIsOk"/>← ← □</global>← (□:SPC , ←:改行)