I am making a registration form.
I need to check if both the password fields are the same.
<validator name="twofields"
classname="com.mysite.StrutsValidator"
method="validateTwoFields"
msg="errors.twofields"/>
<field property="password"
depends="required,twofields">
<arg position="0" key="typeForm.password.displayname"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
Method
public class StrutsValidator {
public static boolean validateTwoFields(
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {
String value = ValidatorUtils.getValueAsString(
bean,
field.getProperty());
String sProperty2 = field.getVarValue("secondProperty");
String value2 = ValidatorUtils.getValueAsString(
bean,
sProperty2);
if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!value.equals(value2)) {
errors.add(field.getKey(),
Resources.getActionError(
application,
request,
va,
field));
return false;
}
}
catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionError( //This line is giving an error.
application,
request,
va,
field));
return false;
}
}
return true;
}
}
I tried this, but it's giving an error. The method getActionError(ServletContext, HttpServletRequest, ValidatorAction, Field)
is undefined for the type Resources.
What is your exact Struts version? At least according to Struts 1.1 API documentation, the Resources.getActionError
method signature is the following:
getActionError(
javax.servlet.http.HttpServletRequest request,
org.apache.commons.validator.ValidatorAction va,
org.apache.commons.validator.Field field
)
This would indicate that you need to remove the application parameter from your method call:
errors.add(
field.getKey(),
Resources.getActionError(
request,
va,
field));