Remove file immediately, if extension is not "PDF"

Certified Associate Developer

HI All,

I am using a!fileUploadField for uploads file, which has validated to allow only "PDF" file, 

Our ask is, if uploaded file extension is not PDF, shall remove immediately, how i can do that?

Note:- file upload feature i have given on a grid column and it's single select.

  Discussion posts and replies are publicly visible

Parents
  • What's the use case for the "immediate removal"? If you implement validations that check for PDF only files, then the User cannot progress with the process which is the actual objective here. Is it just an optimisation you're looking for to save the User from removing the current file before uploading a new one?

  • 0
    Certified Associate Developer
    in reply to Stewart Burchell

    if user tried to upload wrong file (not pdf), instead of showing error message along with file, remove file from field and make it file upload field empty before save

  • The problem here is that there will be no indication to the User what the issue is as the file will have been removed which means the validation logic won't trigger and the User won't seen an error message. The UX at this point is that their file has disappeared with no indication why and they'll think there's a bug in the system and won't trust it. 

    I'm not sure that's a good pattern to be aiming for.

    Systems that Users interact with should follow good "Dialogue" practices. The User asks the system to do something, the system replies accordingly. What you're asking for breaks this good practice.

  • 0
    Certified Associate Developer
    in reply to Stewart Burchell

    actually, my application design is multi pager, if i have applied any validation on certain page and move to another page, in that case, previous page validations not being trigger while saving records.. due to that system allows users to save NON PDF files as well.

  • 0
    Certified Lead Developer
    in reply to nitin07

    But in that case, even if you leave it blank and try to move to the next page, you'll end up not storing any value in it. As the required validation will not trigger if the field is not visible on the screen. 

  • 0
    Certified Associate Developer
    in reply to Harshit Bumb (Appyzie)

    that fine, if no document uploaded but issue only with to upload wrong file (other that PDF)

  • +1
    Certified Lead Developer
    in reply to nitin07

    Use the below code then. 

    a!fileUploadField(
                    label: "File Upload",
                    labelPosition: "ABOVE",
                    value: ri!document_doc,
                    saveInto: {
                      ri!document_doc,
                      a!save(
                        ri!document_doc,
                        if(
                          rule!TA_checkFileExtension(document_doc: ri!document_doc)="PDF",
                          null,
                          ri!document_doc
                        )
                      )
                    },
                    target: cons!DA_DESIGNER_DOCUMENTS,
                  )


    TA_checkFileExtension

    tostring(
      a!fileUploadField(value: ri!document_doc).contents.value.extension
    )

  • Be aware that accessing internal state of components like in TA_checkFileExtension is unsupported and can break without notice any time.

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    This can be done by using the "content details" plug-in which can also access object properties by ID for freshly-uploaded files (though annoyingly the output is only plaintext so we must scrape the result).  I've switched to that method for the times when it's truly necessary.

    /* Parent Rule =  UTIL_getNewUploadDocName */
    a!localVariables(
      local!rawDetails: if(
        a!isNullOrEmpty(ri!doc),
        "",
        getcontentobjectdetailsbyid(ri!doc)
      ),
      local!wholeName: index(extract(
        local!rawDetails,
        "[Name: ",
        ", UUID:"
      ), 1, null()),
    
      rule!UTIL_processDocumentName(
        docName: local!wholeName
      )
    )

    /* Child Rule = UTIL_processDocumentName (uses RegEx plug-in) */
    a!localVariables(
      local!hasNoExtension: search(".", ri!docName) = 0,
      
      local!namePart: if(
        local!hasNoExtension,
        ri!docName,
        regexsearch(
          pattern: ".*(?=\.)",
          searchString: ri!docName,
          regexFlags: "i"
        )[1].match
      ),
      
      local!extension: if(
        local!hasNoExtension,
        "",
        regexsearch(
          pattern: "[^\.]*$",
          searchString: ri!docName,
          regexFlags: "i"
        )[1].match
      ),
      
      a!map(
        name: local!namePart,
        extension: local!extension
      )
    )

    Result:

    cc: please consider adopting this method instead of the unsupported component-hacking that was formerly required for this functionality, at least in terms of what we recommend to people around here Slight smile

  • +1
    Certified Lead Developer
    in reply to nitin07

    I would urge you figure out a way to disable moving between tabs/pages when the validation has an error, implementing a form-level validation component for visibility.  You could use the functionality Harshit and I suggest below to store into a local variable a flag that becomes "true" if any uploaded files have an invalid extension per your business rules.

    I strongly suggest AGAINST simply clearing out the "value" of the File Upload Field if the uploaded document's extension is wrong, because the user experience will be exceptionally poor (they will simply see their uploaded file vanish with no explanation).

Reply
  • +1
    Certified Lead Developer
    in reply to nitin07

    I would urge you figure out a way to disable moving between tabs/pages when the validation has an error, implementing a form-level validation component for visibility.  You could use the functionality Harshit and I suggest below to store into a local variable a flag that becomes "true" if any uploaded files have an invalid extension per your business rules.

    I strongly suggest AGAINST simply clearing out the "value" of the File Upload Field if the uploaded document's extension is wrong, because the user experience will be exceptionally poor (they will simply see their uploaded file vanish with no explanation).

Children
No Data