1
votes

According to this article http://www-10.lotus.com/ldd/ddwiki.nsf/revisions/6A9EDD911827AA13852574EA00388F8F?OpenDocument simple validation should work for File Upload controls. I am trying to use it in a extLib Form table.

I would like to verify that the user have selected a file, but have not been able to get this to work on serverside validation. Have also tried to use a custom validator, but still with no luck. Other required fields are marked fine, but not the upload control.

Do anyone know how validate that the user have actually selected a file?

2

2 Answers

5
votes

The validation works for client side validation only. There are some workarounds:

  1. The easiest way to validate if a file was attached is to add a validation field to your form and set the property computeWithForm="onsave" of your datasource. As soon as you want to save the document a validation error is thrown and the saving is interrupted. The validation field is a simple editable field with a validation formula like this:

    @If(@Attachments = 0;@Failure("No File attached!");@Success)
    
  2. Check your datasource in the querySave event:

    if( document1.getAttachmentList("Body").isEmpty() ){
        var msg = new javax.faces.application.FacesMessage("No File added!");
        facesContext.addMessage( "No File!", msg );
        return false;
    }
    

These two workarounds are only working if the document is newly created. As soon a file is attached, these two options are not working anymore.

If you want to check already existing documents, you can use this XSnippet here: http://openntf.org/XSnippets.nsf/snippet.xsp?id=replace-attachment-when-uploading-a-new-attachment

You then have to modify the XSnippet to fit your requirements and add a message (as shown in the second example).

Hope this helps

Sven

0
votes

I'm aware that this has been asked and answered several months ago, but I was looking for an answer to the same problem today when I found this.

Although Sven's answers didn't help directly, option #2 gave the final hint to my solution. Maybe it can be of use for others, too:

First of all, my page uses a standard button (not a button of type "Submit" as I need to set some hidden fields along with the editable ones). So, before the final saving is done I added this script to my button code:

var numAtts = myDocDatasource.getAttachmentList("Body").size();
if(numAtts == 0){
    var msg = new javax.faces.application.FacesMessage("You need to attach a file");
    facesContext.addMessage("File validation error", msg);
    return false;
}
//do some more stuff
...
myDocDatasource.save();

I had to realize that the content of the fileUpload control doesn't really matter when it comes to validation as at that stage of the process an uploaded file already is part of the datasource.

The "timing" of this validation step is a bit surprising, though: at least in my situation, validation of other fields is done before the file upload is validated: in an errorMessages control first only the standard validation errors are listed. Only after all the other fields have been validated successfully my fileUpload validator is displaying its error.