My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://utku-utkan.appspot.com/
and update your bookmarks.

Pages

Thursday, July 26, 2007

Struts File Validator

Struts Validator Framework provides the functionality to validate form data. It can be used to validate data on the user's browser as well as on the server side, and it ships with pre-defined validators, such as: required, requiredif, validwhen, minlength, maxlength, mask, byte, short, integer, long, float, double, date, range, intRange, floatRange, doubleRange, creditCard, email, url. Now, we will develop four more validators to validate file fields which are requiredFile, minFileSize, maxFileSize and fileContent.

Struts File Validator which is an extension for Struts Validaor Framework is composed of two utility classes FormFileValidator and FileFieldCheks. FormFileValidator is independent from Struts and it provides the actual validation methods. On the other hand, FileFieldChecks uses these validation methods to supply the validation routines for the Struts Framework.

The Validator framework is set up as a pluggable system of validation routines that can be applied to Form Beans. In order to use File Validation routines in your Struts application, they must be loaded first through validator-rules.xml file. Configuration elements for requiredFile, minFileSize, maxFileSize and fileContent validators are as follows:

<validator name="requiredFile"
   classname="com.coresun.commons.struts.validator.FileFieldChecks"
   method="validateRequiredFile"
   methodParams="java.lang.Object,
      org.apache.commons.validator.ValidatorAction,
      org.apache.commons.validator.Field,
      org.apache.struts.action.ActionMessages,
      org.apache.commons.validator.Validator,
      javax.servlet.http.HttpServletRequest"
   msg="errors.requiredfile"/>


<validator name="minFileSize"
   classname="com.coresun.commons.struts.validator.FileFieldChecks"
   method="validateMinFileSize"
   methodParams="java.lang.Object,
      org.apache.commons.validator.ValidatorAction,
      org.apache.commons.validator.Field,
      org.apache.struts.action.ActionMessages,
      org.apache.commons.validator.Validator,
      javax.servlet.http.HttpServletRequest"
   msg="errors.minfilesize"/>


<validator name="maxFileSize"
   classname="com.coresun.commons.struts.validator.FileFieldChecks"
   method="validateMaxFileSize"
   methodParams="java.lang.Object,
      org.apache.commons.validator.ValidatorAction,
      org.apache.commons.validator.Field,
      org.apache.struts.action.ActionMessages,
      org.apache.commons.validator.Validator,
      javax.servlet.http.HttpServletRequest"
   msg="errors.maxfilesize"/>


<validator name="fileContent"
   classname="com.coresun.commons.struts.validator.FileFieldChecks"
   method="validateFileContent"
   methodParams="java.lang.Object,
      org.apache.commons.validator.ValidatorAction,
      org.apache.commons.validator.Field,
      org.apache.struts.action.ActionMessages,
      org.apache.commons.validator.Validator,
      javax.servlet.http.HttpServletRequest"
   msg="errors.filecontent"/>


Once these configuration elements are added to the validatitor-rules.xml, you are ready validate file fields of the Form Beans. The following describes the validation routines supplied by Struts File Validator extension.
  • requiredFile: Mandatory file field validation. Has no variables.

<field property="picture" depends="requiredFile">
   <arg0 key="customer.picture"/>
</field>

  • minFileSize: Validate size of the file isn't less than a specified minimum size. Requires a minFileSize variable and a unit variable. unit variable should be KB, MB or GB.

<field property="picture" depends="requiredFile,minFileSize">
   <arg0 key="customer.picture"/>
   <arg1 key="${var:min}" resource="false"/>
   <arg2 key="${var:unit}" resource="false"/>
   <var>
      <var-name>min</var-name>
      <var-value>10</var-value>
   </var>
   <var>
      <var-name>unit</var-name>
      <var-value>KB</var-value>
   </var>
</field>

  • maxFileSize: Validate size of the file doesn't exceed a specified maximum size. Requires a maxFileSize variable and a unit variable. unit variable should be KB, MB or GB.

<field property="picture" depends="requiredFile,maxFileSize">
   <arg0 key="customer.picture"/>
   <arg1 key="${var:max}" resource="false"/>
   <var>
      <var-name>max</var-name>
      <var-value>1</var-value>
   </var>
   <var>
      <var-name>unit</var-name>
      <var-value>MB</var-value>
   </var>
</field>

  • fileContent: Validate content type of the file. Requires a fileContents variable that is composed of comma separated list of the valid content types.

<field property="picture" depends="requiredFile,fileContent">
   <arg0 key="customer.picture"/>
   <var>
      <var-name>contentTypes</var-name>
      <var-value>image/jpeg</var-value>
   </var>
</field>


And finally, error messages for these validators should be added to the MessageResources.properties file.

errors.requiredfile={0} is required.
errors.minfilesize={0} can not be less than {1} {2}.
errors.maxfilesize={0} can not be greater than {1} {2}.
errors.filecontent={0} is not a valid file.


You can either download the binary or the source distribution of Struts File Validator extension.

No comments: