You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Franz-Josef Herpers <of...@fjh-consulting.de> on 2005/08/04 12:42:46 UTC

Custom Validation doesn't add

Hi,

I ran into a strange problem with the validation of form input that 
includes an uploaded file in Struts 1.2.7. I wrote a custom validation 
method for validating the file size of an uploaded file:

  public static boolean validateFile(Object bean,
                                    ValidatorAction validatorAction,
                                    Field field,
                                    ActionMessages messages,
                                    Validator validator,
                                    HttpServletRequest request) {
   
    Object o = 
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
    Boolean fileLengthExceeded = (Boolean) o;

    if ((fileLengthExceeded != null) && fileLengthExceeded.booleanValue()) {
      messages.add(field.getKey(),
                 Resources.getActionMessage(request, validatorAction, 
field));
      return false;
    }
 
    return true;

  }

I defined a validation rule in my validator-rules.xml:

    <validator name="fileSize"
               
classname="org.uba.iuclidx.gui.web.struts.validators.FormValidator"
               method="validateFile"
               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.maxLengthFileExceeded"/>

And I added an entry for the form to be validated in validation.xml

  <form name="iuclidExportForm">
      <field property="casNo" depends="required">
        <msg name="required" key="casNo.required"/>
      </field>
      <field property="blockTableNames" depends="required">
        <msg name="required" key="blockTableNames.required"/>
      </field>
      <field property="mapDescrFile" depends="required,fileSize">
        <msg name="required" key="mapDescrFile.required"/>
        <msg name="fileSize" key="maxLengthMapDescrFileExceeded"/>
      </field>
    </form>

Everything works fine, and the method is called. But if the file exceeds 
the file size defined in the controller element of struts-config.xml, I 
get the error messages of all fields for the required validation rule 
(including the file upload field!), but not the one for exceeding the 
file size. Additionally the fields contain no more values. As if the 
form has been reset after executing validateFile and then validated 
again.....

Any ideas or hints?

Thanks in advance.
Franz


-- 
Franz-Josef Herpers

fjh consulting
Tel.: +49 30 2068 7154
Mobil: +49 173 54 23 666
VoIP: +49 2222 948 625515
Fax: 01212 6 20687154
Puschkinallee 9A
12435 Berlin

Email: Franz-Josef.Herpers@fjh-consulting.de
VoiceMail: 01212-6-20687154

http://www.fjh-consulting.de
__________________________________________________
http://www.openbc.com/go/invuid/FranzJosef_Herpers



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org