In The Struts
Validator Guide, there is a section on how to create a pluggable validator that matches two fields. I've been using this server-side validator (as shown in the example) to do password / confirm password validation. This has worked great for me , But I've Always Wanded-Side Javascript Method for It Too. I Wrote My OWN That Just Compared The Two Fields, But It's NOT The Same As Having One Rendered for you (from from
Validator-rules.xml). So Yesterday, I Did Some Tinkering and first how to add the javascript method
Validator-rules.xml. So Here's How To Configure The Whole Thing (Most of this Is Contained in The Validator Guide, Save the JavaScript).
How to Add A Twofields Validator
Step 1: CREATE A Class with a
Validatetwofields Method. in My Code, My Class IS
ValidationUtil and Has The Following Method:
Public Static Boolean ValidateTwofields (Object Bean, ValidatorAction VA,
Field Field, Actionerrors Errors,
HttpservletRequest request) {
String value =
ValidatorUtil.getValueAsstring (bean, field.getproperty ());
String Sproperty2 = Field.getvarvalue ("SECONDPROPERTY");
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;
}
Step 2: Edit Validator-Rules.xml to Contain The "Twofields" Rule.
ClassName = "Org.Appfuse.Webapp.util.validationUtil" Method = "ValidateTwofields" MethodParams = "java.lang.object, Org.apache.commons.valiDator.validatorAction, Org.apache.commons.validator.field, Org.apache.struts.Action.Actionerro, Javax.Servlet.http.httpservletRequest " Depends = "Required" msg = "errors.twofields"> Function ValidateTwofield (Form) { Var bvalid = true; Var Focusfield = NULL; VAR i = 0; Var fields = new arrival (); OTWOFIELDS = New Twofields (); For (x in otwofields) { Var Field = form [otwofields [x] [0]]; VAR SecondField = form [otwofields [x] [2] ("SECONDPROPERTY")]; IF (Field.Type == 'Text' || Field.Type == 'Textarea' || Field.Type == 'Select-one' || Field.Type == 'Radio' || Field.Type == 'password') { VAR value; VAR SecondValue; // Get Field's Value IF (Field.Type == "Select-One") { Var si = field.selectedIndIndex; Value = Field.Options [Si] .value; Secondfield.Options [Si] .value; } else { Value = Field.Value; Secondfield.Value; } IF (value! = secondValue) { IF (i == 0) { Focusfield = field; } Fields [i ] = OTWOFIELDS [X] [1]; BVALID = false; } } } IF (Fields.Length> 0) { Focusfield.focus (); Alert (Fields.Join ('/ n')); } Return BVALID; }]> javascript> validator> Step 3: Configure Validation for your form in value.xml: Depends = "Required, Twofields"> name = "Required" key = "errors.required" /> Name = "twofields" Key = "errors.twofields" /> Key = "Userform.confirmpassword" />
var> field> Where errors.twofields = The '{0}' field has to have the same value as the '{1}' field. An alternative to Step 3 is to use XDoclet to generate your validation.xml. This requires (1) configuring XDoclet (of course) and (2) adding some @struts tags to your form on the setpassword method. / ** * Returns the password. * @Return String * * @ Struts.validator type = "required" msgkey = "errors.required" * @ Struts.validator type = "twofields" msgkey = "errors.twofields" * @ Struts.validator-args arg1resource = "userform.password" * @ Struts.validator-args arg1resource = "userform.confirmpassword" * @ Struts.validator-var name = "secondproperty" value = "confirmpassword" * / Public string setpassword () { Return Password; } I've sent this as a proposal to the struts-dev mailing list yesterday, but have not heard anything yet Enjoy Update:.! You'll need to update ValidationUtil.java and validator-rules-custom.xml for Struts 1.2. Full Files: ValidationUtil.java and validation-rules-custom.xml. February 26, 2003 12:29 PM MST Permalink 10 Comments