Subject: Re: Dependency Validations. From: "Anand Jayaraman" <[EMAIL PROTECTED]> === Thanks Drago. I will try it.
"Drago Jenko" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]... > Try this! > > > On jsp you have id_field1 and id_field2 > > This validation checks, if id_field1 is empty then id_field2 must be filled > or > if id_field2 is empty, then id_field1 must be filled. > > > In validator.xml: > > <field property="id_field1" depends="checktwofields"> > <arg0 key="error.checktwofields"/> > <arg1 key="error.id_field1"/> > <arg2 key="error.id_field2"/> > <var> > <var-name>secondProperty</var-name> > <var-value>id_field2</var-value> > </var> > </field> > > In resource file you must have: > > errors.checktwofields={0} Some text ... "{1}" or "{2}"! > > > In validator-rules.xml: > > <validator name="checktwofields" > classname="your.package.CheckTwoFields" > method="checkTwoFields" > methodParams="java.lang.Object, > org.apache.commons.validator.ValidatorAction, > org.apache.commons.validator.Field, > org.apache.struts.action.ActionErrors, > javax.servlet.http.HttpServletRequest" > msg="errors.checktwofields"> > </validator> > > In your.package.CheckTwoField: > > package your.package; > > import java.io.Serializable; > > 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.ValidatorUtil; > import org.apache.struts.action.ActionErrors; > import org.apache.struts.util.StrutsValidatorUtil; > > public class CheckTwoFields implements Serializable { > > public static boolean checkTwoFields( > 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); > > try { > if ((!GenericValidator.isBlankOrNull(value)) > && (!GenericValidator.isBlankOrNull(value2))) { > errors.add( > field.getKey(), > StrutsValidatorUtil.getActionError(request, va, field)); > errors.add( > sProperty2, > StrutsValidatorUtil.getActionError(request, va, field)); > > return false; > } > if ((GenericValidator.isBlankOrNull(value)) > && (GenericValidator.isBlankOrNull(value2))) { > errors.add( > field.getKey(), > StrutsValidatorUtil.getActionError(request, va, field)); > errors.add( > sProperty2, > StrutsValidatorUtil.getActionError(request, va, field)); > > return false; > } > return true; > } > catch (Exception e) { > errors.add( > field.getKey(), > StrutsValidatorUtil.getActionError(request, va, field)); > errors.add( > sProperty2, > StrutsValidatorUtil.getActionError(request, va, field)); > return false; > } > > } > > } > ----- Original Message ----- > From: "Struts Newsgroup" <@[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Friday, May 31, 2002 1:55 AM > Subject: Dependency Validations. > > > > Subject: Dependency Validations. > > From: "Anand Jayaraman" <[EMAIL PROTECTED]> > > === > > Im using Struts 1.1 commons validator xml. > > Can someone show me a example as how to validate a field based on another > > fields presence. > > > > Basically if Field1 is present I want to validate Field2, if not proceed. > > > > Thanks, > > > > > > > > > > -- > > To unsubscribe, e-mail: > <mailto:[EMAIL PROTECTED]> > > For additional commands, e-mail: > <mailto:[EMAIL PROTECTED]> > > > > > -- > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> > -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>