• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Struts custom validator for multiple properties

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is it possible to validate multiple textboxes on a jsp using struts validator without having to specify each property in the validator.xml?
I have a jsp which has 20 textboxes (currQuarter1,currQuarter2,..prevQuarter1...etc)
I want to validate all of them so that the user may only enter numerical values. I am using the mask - ^[0-9]*$ and it works fine if i explicily specify the property name in the validation.xml
eg -
<form name="CheckWageCriteriaForm">
<field property="currQtr1" depends="mask">
<msg name="mask" key="CheckWageCriteriaForm.currQtr1.maskmessage"/>
<arg key="CheckWageCriteriaForm.currQtr1"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
</form>

However, I have 20 properties and i dont want to write the form tag for all of them since they have the same validation.
So, i wrote a custom validation class, and added it to validation-rules.xml.

WagePaymentsValidator Class :-
public class WagePaymentsValidator implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

public static boolean validateAllQtrs(
Form form,
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {

//String value = ValidatorUtils.getValueAsString(
//bean,
//field.getProperty());
try
{
String currYearQtr = null;
List<String> currQtrList = new ArrayList<String>();
String prevYearQtr = null;
List<String> prevQtrList = new ArrayList<String>();

for(int i=0;i<4;i++)
{
currYearQtr = "currQtr"+i;
field = form.getField(currYearQtr);
String sProperty = field.getVarValue(currYearQtr);
String currQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(currQtrValue))
{
if(!containsOnlyNumbers(currQtrValue))
return false;
}
//currQtrList.add(currQtrValue);
}
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;i++)
{
prevYearQtr = "prev"+i+"Qtr"+j;
field = form.getField(prevYearQtr);
String sProperty = field.getVarValue(prevYearQtr);
String prevQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(prevQtrValue))
{
if(!containsOnlyNumbers(prevQtrValue))
return false;
}
//prevQtrList.add(prevQtrValue);
}

}
}
catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}

return true;

}
/**
* This method checks if a String contains only numbers
*/
private static boolean containsOnlyNumbers(String str) {

//Check for null or empty fields
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}

}

validation-rules.xml :-
<!--Custom Validator for Wage Payments page-->
<validator name="validateAllQtrs"
classname="gov.iowa.wd.ui.employer.determineliability.WagePaymentsValidator"
method="validateAllQtrs"
methodParams="org.apache.commons.validator.Form,
java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="CheckWageCriteriaForm.currQtr1.maskmessage"/>

However, the system does not validate the fields and if i enter a non-numerical value in any of the textboxes and submit, i get numberformatexception in my ejb method as it can handle only numeric values.

Is there any other way I can achieve validation for multiple textboxes?
 
Ashwini Kulkarny
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
validation.xml :-
<form name="CheckWageCriteriaForm">
<field depends="validateAllQtrs">
<msg name="mask" key="CheckWageCriteriaForm.currQtr1.maskmessage"/>
<arg key="CheckWageCriteriaForm.currQtr1"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
</form>
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you need to specify all the form fields individually. For an example on this refer here. Struts Mask Validation
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic