Tkanks, Jiri
David Stemm wrote:
Jiri, I have logic that does this. Add this to your validator-rules.xml:
<validator name="twoFieldsEqual" classname="com.yourcompany.StrutsValidator" method="validateTwoFieldsEqual" msg="errors.twoFieldsEqual" depends="required" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest"> <javascript><![CDATA[ function validateTwoFieldsEqual(form) { var isValid = true; var focusField = null; var i = 0; var fields = new Array(); oTwoFields = new twoFieldsEqual(); for (x in oTwoFields) { var field = form[oTwoFields[x][0]]; var secondField = form[oTwoFields[x][2]("equalField")];
if (field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'radio' || field.type == 'password') {
//alert('field is ' + field + ' and field value is ' + field.value); //alert('field2 is ' + field2 + ' and field2 value is ' + field2.value);
var value; var secondValue; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; value = field.options[si].value; secondValue = secondField.options[si].value; } else { value = field.value; secondValue = secondField.value; }
if (value != secondValue) { if (i == 0) { focusField = field; } fields[i++] = oTwoFields[x][1]; isValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return isValid; }]]></javascript> </validator>
and this to your validation.xml:
<field property="password2" depends="required, twoFieldsEqual"> <arg0 key="prompt.password2"/> <arg2 key="prompt.password"/> <var> <var-name>equalField</var-name> <var-value>password</var-value> </var> </field>
and a method like this in your StrutsValidator class:
public static boolean validateTwoFieldsEqual(Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { String value = ValidatorUtil.getValueAsString(bean, field.getProperty()); String sProperty2 = field.getVarValue("equalField"); String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
if (!GenericValidator.isBlankOrNull(value))
{
try
{
if (!value.equals(value2))
{
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
}
} catch (Exception e)
{
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
}
}
return true;
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]