Hi,

I have created a validator called "SameValueValidator", this worked both client-side and server side, this was for struts2.0.x so I don't know
how will work with the latest struts (and if it works with annotations).

If You are interested here it is:
SameValueValidator.java:
--------------------------
public class SameValueValidator extends FieldValidatorSupport
{
   private String  otherField;
   @Override
   public void validate(Object object) throws ValidationException
   {
       String fieldName = getFieldName();
       String val = (String) getFieldValue(fieldName, object);
       if (val == null || val.length() <= 0) {
           // use a required validator for these
           return;
       }
       String otherVal=(String) getFieldValue(otherField, object);
       if(!val.equals(otherVal))
       {
           addFieldError(fieldName, object);
       }
   }
   public String getOtherField()
   {
       return otherField;
   }
   public void setOtherField(String otherField)
   {
       this.otherField = otherField;
   }
}
--------------------------
How to use it in xml:
<field name="password2">
<field-validator type="samevalue" >
   <param name="otherField">login.userPasswd</param>
   <message key="error.passwordmatch" />
</field-validator>
</field>

Add it as Validator is validators.xml:
<validator name="samevalue" class="yourpackage.validators.SameValueValidator"/>

Changes required for client-side validation (javascript):
file: template\xhtml\form-close-validate.ftl:

(there are a bunch of elseifs, add this at the end):
-----------------------------------------
<#elseif validator.validatorType = "samevalue">
   if (field.value != null) {
       var otherField=form.elements['${validator.otherField}'];
       if (field.value!=otherField.value) {
           addError(field, error);
           errors = true;
       }
   }
</#if>
-----------------------------------------

Regards,
Andras.

James Carr wrote:
Hello,

Is there something out of the box that will let me validate if two
fields match in struts2? An example would be a form with email and
confirmingEmail fields.

Thanks,
James

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to