[SalesForce] problem with uploading document .. missing fields

I have a page with uploading document form, I followed this example but I got a problem when I try to upload the file with the following error message Required fields are missing: [Document Name]

And here is my VF code

<apex:pageBlock title="Scan Document" >
  <apex:form id="scanDocumentTabForm" enctype="multipart/form-data">
            <apex:pageBlockSection columns="1" >
                  <apex:inputText value="{!document.name}"
                                  id="fileName"/>
                  <apex:inputFile value="{!document.body}"
                                  filename="{!document.name}"
                                  id="file"/>
                  <apex:inputField label="Description" value="{!document.description}"/>
                  <apex:actionRegion >
                       <apex:commandButton value="Upload File"
                                           action="{!saveDocument}"
                                           reRender="documentsTableForm"
                                           status="getDoc"/>
                  </apex:actionRegion>

            </apex:PageBlockSection>

            <apex:actionStatus startStyleClass="loading"
                               stopStyleClass="unload"
                               id="getDoc"/>

            <apex:pageMessages /> 
            <apex:pageBlockSection >
                  <apex:panelGroup id="documentsTable">
                        <apex:pageBlockTable value="{!documents}" var="doc" >
                             // table content .. 
                                    <!-- This is the dynamic reference part -->                    
                        </apex:pageBlockTable>
                  </apex:panelGroup>
            </apex:pageBlockSection>
    </apex:form>
</apex:pageBlock>

And this is the Controller Code

public Document document {get; set;}

public MyController (){
        document = new Document();
}

public pageReference saveDocument() {

        document.AuthorId = UserInfo.getUserId();
        document.FolderId = UserInfo.getUserId();

        try {
            insert document;

        } catch (DMLException e) {        
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));

        return null;
        } finally {
          document.body = null; // clears the viewstate
          document = new Document();
        }

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));

        return null;
    }

Best Answer

Re-render don't work with <apex:input> but there is is no problem in page reload. On page refresh no information lost and data changes during action method call these details can be used on page

Here is an example using above sample code:

    public class DocumentUploader {
    public Document document {get; set;}
    public List<document> documents {get; set;}

    public DocumentUploader (){
            document = new Document();
            // Will show 3 results
            documents = [SELECT id, name, description FROM Document LIMIT 3];
    }

    public pageReference saveDocument() {

            document.AuthorId = UserInfo.getUserId();
            document.FolderId = UserInfo.getUserId();

            try {
                insert document;

            } catch (DMLException e) {        
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));

            return null;
            } finally {
              document.body = null; // clears the viewstate
              document = new Document();
              // This will load all document and refresh page. Constructor not reloads
              documents = [SELECT id, name, description FROM Document];
            }

            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));

            PageReference pref = ApexPages.currentPage();
            pref.setRedirect(false);
            return pref;
        }
}

Page:

<apex:page controller="DocumentUploader" tabstyle="Document">
<apex:form>
 <apex:pageblock>
    <apex:pageBlockSection columns="1" >
         <apex:pagemessages/>
         <apex:inputField value="{!document.name}" id="fileName"/>
         <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
         <apex:inputField label="Description" value="{!document.description}"/>
         <apex:commandButton value="Upload File" action="{!saveDocument}" status="getDoc" />
    </apex:PageBlockSection>
    <apex:pageblockTable value="{!documents}" var="doc">
      <apex:column value="{!doc.name}"/>
      <apex:column value="{!doc.description}"/>
    </apex:pageblockTable>
  </apex:pageblock>
 </apex:form>
</apex:page>
Related Topic