0
votes

I've created an online application which works well as long as someone has uploaded a resume. We now have positions available where a resume is optional, but the system throws an error if there's no file in the que.

I've tried IsDefined and StructKeyExists and can't get it to work. Any suggestions are greatly appreciated...

<cfset destination = expandPath("./uploads")>
<cfif IsDefined('form.submitapp')>
        <cfif IsDefined("form.uploadfile")>
            <cffile action="upload" filefield="uploadfile" destination="#destination#" nameConflict="makeUnique">
            <cfif isDefined("CFFILE.serverFile")>
            <cfset form.resume = CFFILE.serverFile>
            </cfif>
        </cfif>

  <cfinsert tablename=...
1
Share your form code. Always share your form code. Do you have a cfparam for form.submitapp and form.uploadfile? If so, that's likely your problem. - Regular Jo
Also, keep in mind "empty" is a different concept than "undefined". Most (not all) form fields will be submitted, regardless of whether they contain a value. That means they will be defined on the action page. They will just have a value of an empty string ie "". Functions like structKeyExists do not check the field value. They check whether a field is defined. So be sure you are using the correct comparisons for the job. - Leigh
The fact that you are using cfinsert instead of cfquery reduces your flexibility for handling things like optional form fields. - Dan Bracuk
Also, once you resolve this, because this is an easily solvable problem, it's a good idea to put the CFFILE and subsequent cfset inside a CFTRY/Catch like <cftry><cffile...><cfset....><cfcatch>An error with the upload occurred.</cfcatch></cftry>. Resolve this problem first so you're not trapping a simple logical error. - Regular Jo

1 Answers

0
votes

As bracketsage said - you probably have a default in place. Try this:

<cfset destination = expandPath("./uploads")>
<cfif IsDefined('form.submitapp')>
  <cfif IsDefined("form.uploadfile") AND len(trim(form.uploadFile))>
  <cffile action="upload" filefield="uploadfile" destination="#destination#" nameConflict="makeUnique">
    <cfif isDefined("CFFILE.serverFile")>
      <cfset form.resume = CFFILE.serverFile>
    </cfif>
  </cfif>
</cfif>