The Struts framework features a built-in database for performing data validations on incoming form data. Its simple interface means that developers can focus on writing validation code; additionally, you can plug your own custom routines into Validator. This article shows you how. It is taken from chapter six of the book The Complete Reference Struts, written by James Holmes (McGraw-Hill/Osborne, 2004; ISBN: 0072231319).
Validator - Adding a New Validation Rule (Page 7 of 9 )
After the custom validation code has been created, a new validation rule needs to be added to the validator-rules.xml file. As discussed earlier in this chapter, validation rules “plug” validation methods into the Validator. Once defined in validator-rules.xml, as shown here, the validation rule can be referenced in the validation.xml file:
<validator name="ssNum" classname="com.jamesholmes.minihr.MiniHrValidator" method="validateSsNum" 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.ssNum"> <javascript> <![CDATA[ function validateSsNum(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oSsNum = new ssNum(); for (x in oSsNum) { if ((form[oSsNum[x][0]].type == 'text' || form[oSsNum[x][0]].type == 'textarea') && (form[oSsNum[x][0]].value.length > 0)) { var value = form[oSsNum[x][0]].value; var bRightFormat = true; if (value.length != 11) { bRightFormat = false; } for (var n = 0; n < 11; n++) { if (n == 3 || n == 6) { if (value.substring(n, n+1) != '-') { bRightFormat = false; } } else if ("0123456789".indexOf( value.substring(n, n+1)) == -1) { bRightFormat = false; } } if (!bRightFormat) { if (i == 0) { focusField = form[oSsNum[x][0]]; } fields[i++] = oSsNum[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } ]]> </javascript> </validator>
As you can see, the validation rule applies a name to the validation with the name attribute; specifies the class that the validation method is housed in with the classname attribute; and specifies the validation method’s arguments with the methodParamsattribute. The msg attribute specifies a key that will be used to look up an error message in the ApplicationResources.properties file if the validation fails. Note that the name applied with the nameattribute is the logical name for the rule and will be used to apply the rule to definitions in the validation.xml file.
The preceding custom validation rule also defines JavaScript code, inside the opening and closing <javascript>elements, which will be used if client-side validation is enabled when using the rule. The JavaScript code simply performs the same validation on the client side as is performed on the server side. If the JavaScript validation fails, it will alert the user and prevent the HTML form from being submitted.
Note that validation rules must be placed underneath the <global> element in the validator-rules.xmlfile, as shown here:
Of course, the order of the <validator>elements underneath the <global>element is arbitrary, so you can place the new “ssnum” validation rule anywhere.
Adding New Validation Definitions
Once you have defined your custom validation rule in the validator-rules.xml file, you can make use of it by referencing its logical name in the validation.xml file:
In the preceding validation definition, each of the comma-delimited values of the field tag’s depends attribute corresponds to the logical name of a validation rule defined in the validation-rules.xmlfile. The use of “ssNum” instructs Validator to use the custom Social Security Number validation. Each time you want to use the new Social Security Number validation, you simply have to add its logical name to the dependsattribute of a fieldtag.
Adding Messages to the ApplicationResources.properties File
The final step in creating a custom validation is to add messages to the ApplicationResources.properties file:
prompt.ssNum=Social Security Number errors.ssNum={0} is not a valid Social Security Number
Remember that the errors.ssNummessage key was specified by the msg attribute of the validator tag for the custom validation rule in the validator-rules.xml file. The key’s corresponding message will be used if the validation fails. The prompt.ssNum message key was specified by the arg0tag of the validation definition in the validation.xmlfile. Its corresponding message will be used as the parametric replacement for the errors.ssNummessage’s {0} parameter. Thus, if the Social Security Number custom validation fails, the following error message will be generated by substituting the prompt.ssNummessage for {0} in the errors.ssNummessage:
Social Security Number is not a valid Social Security Number