You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by grifoxx <pg...@gmail.com> on 2006/10/07 01:51:23 UTC
Validate two fields
Hi I am trying to validate to password field so I want to check if they have
the same values.
This my StrutsValidator Class:
package com.sfv;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.*;
import org.apache.struts.validator.Resources;
public class StrutsValidator {
//~ Methods
================================================================
/**
* Validates that two fields match.
* @param bean
* @param va
* @param field
* @param errors
* @param request
* @return boolean
*/
public static boolean validateTwoFields(Object bean, ValidatorAction va,
Field field, ActionErrors errors,
HttpServletRequest request) {
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(request, va, field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
}
}
return true;
}
}
this is my validator rule:
<validator name="twofields"
classname="com.sfv.StrutsValidator"
method="validateTwoFields"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required" msg="errors.twofields"/>
and this is what I have i the validation.xml file:
<formset>
<form name="/viewupdateduserpassword">
<field property="password"
depends="required, twofields">
<msg name="required" key="errors.required"/>
<msg name="twofields" key="errors.twofields"/>
<arg0 key="inputForm.password"/>
<arg1 key="inputForm.passwordConfirmed" />
<var><var-name>secondProperty</var-name><var-value>passwordConfirmed</var-value></var>
</field>
<field property="passwordConfirmed"
depends="required">
<arg0 key="inputForm.passwordConfirmed"/>
</field>
</form>
</formset>
the problem is that is not validating anything.
Can anybody has a suggestion
--
View this message in context: http://www.nabble.com/Validate-two-fields-tf2398749.html#a6689135
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Validate two fields
Posted by Laurie Harper <la...@holoweb.net>.
Have you looked at the validwhen rule? That can do what you want without
the need for a custom validation.
L.
grifoxx wrote:
> Hi I am trying to validate to password field so I want to check if they have
> the same values.
>
> This my StrutsValidator Class:
>
> package com.sfv;
>
> import javax.servlet.http.HttpServletRequest;
>
> import org.apache.commons.validator.Field;
> import org.apache.commons.validator.GenericValidator;
> import org.apache.commons.validator.ValidatorAction;
> import org.apache.commons.validator.util.ValidatorUtils;
> import org.apache.struts.action.*;
> import org.apache.struts.validator.Resources;
>
> public class StrutsValidator {
> //~ Methods
> ================================================================
>
> /**
> * Validates that two fields match.
> * @param bean
> * @param va
> * @param field
> * @param errors
> * @param request
>
> * @return boolean
> */
> public static boolean validateTwoFields(Object bean, ValidatorAction va,
> Field field, ActionErrors errors,
> HttpServletRequest request) {
> 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(request, va, field));
>
> return false;
> }
> } catch (Exception e) {
> errors.add(field.getKey(),
> Resources.getActionError(request, va, field));
>
> return false;
> }
> }
>
> return true;
> }
> }
>
>
> this is my validator rule:
>
> <validator name="twofields"
> classname="com.sfv.StrutsValidator"
> method="validateTwoFields"
> methodParams="java.lang.Object,
> org.apache.commons.validator.ValidatorAction,
> org.apache.commons.validator.Field,
> org.apache.struts.action.ActionErrors,
> javax.servlet.http.HttpServletRequest"
> depends="required" msg="errors.twofields"/>
>
> and this is what I have i the validation.xml file:
> <formset>
> <form name="/viewupdateduserpassword">
>
> <field property="password"
> depends="required, twofields">
> <msg name="required" key="errors.required"/>
> <msg name="twofields" key="errors.twofields"/>
> <arg0 key="inputForm.password"/>
> <arg1 key="inputForm.passwordConfirmed" />
>
> <var><var-name>secondProperty</var-name><var-value>passwordConfirmed</var-value></var>
> </field>
>
> <field property="passwordConfirmed"
> depends="required">
> <arg0 key="inputForm.passwordConfirmed"/>
> </field>
>
> </form>
>
> </formset>
>
> the problem is that is not validating anything.
>
> Can anybody has a suggestion
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org