4 Replies Latest reply on Oct 22, 2010 2:26 PM by insuractive

    Soo close. Looping over certian form elements to see if they are empty

    wmkolcz Level 1

      I have a form that gets split into 2 actions. Part is emailed, part is databased. Didn't think of it at the time, since the part of the form is shared, no matter if the parts that are databased are empty, it still gets inserted. What I need to do is check about 11 fields to see if they are blank. If not, then do an insert, if so, then don't do anything.

       

      What I thought would work best was create an array of the fields that I wanted checked. Then loop over the form.(element) and see if it is not blank. If it isn't, then do the insert and stop checking. I tried this, but am getting errors. Any ideas? Would one long cfif statement with a bunch of OR's be better?

       

       

      <cfset ffields = ArrayNew(1) />
      <cfset ffields[12] = "degree" />
      <cfset ffields[1] = "title" />
      <cfset ffields[2] = "dept2" />
      <cfset ffields[3] = "spec" />
      <cfset ffields[4] = "location" />
      <cfset ffields[5] = "addr" />
      <cfset ffields[6] = "city" />
      <cfset ffields[7] = "zip" />
      <cfset ffields[8] = "phone" />
      <cfset ffields[9] = "medschool" />
      <cfset ffields[10] = "baordcerts" />
      <cfset ffields[11] = "otherChanges" />

       

          <cfloop index="x" from="1" to="#arrayLen(ffields)#">
          <cfset fname = #ffields[x]# />
              <cfif form.#fname# NEQ "">
                  Do and insert
                  <cfbreak />
              </cfif>
          </cfloop>

        • 1. Re: Soo close. Looping over certian form elements to see if they are empty
          Dan Bracuk Level 5

          I do stuff like that like this:

           

          <cfloop list = "#form.fieldlist#" index= "ThisField">

          <cfset ThisValue = form["#ThisField#">

           

          Then apply whatever logic is necessary.

          • 2. Re: Soo close. Looping over certian form elements to see if they are empty
            ilssac Level 5

            Dan Bracuk wrote:

             

            <cfset ThisValue = form["#ThisField#"]>

             

            You can even get rid of a few characters if you like.  This works just as well.

             

            <cfset ThisValue = form[ThisField]>
            
            • 3. Re: Soo close. Looping over certian form elements to see if they are empty
              BKBK Adobe Community Professional & MVP

              Two things. First, from what you've described ("check about 11 fields to see if they are blank. If not, then do an insert, if so, then don't do anything"), there should be no <cfbreak>. Your code should read like

               

              <cfset ffields = ArrayNew(1) />
              <cfset ffields[12] = "degree" />
              <cfset ffields[1] = "title" />
              <cfset ffields[2] = "dept2" />
              <cfset ffields[3] = "spec" />
              <cfset ffields[4] = "location" />
              <cfset ffields[5] = "addr" />
              <cfset ffields[6] = "city" />
              <cfset ffields[7] = "zip" />
              <cfset ffields[8] = "phone" />
              <cfset ffields[9] = "medschool" />
              <cfset ffields[10] = "baordcerts" />
              <cfset ffields[11] = "otherChanges" />

               

                  <cfloop index="x" from="1" to="#arrayLen(ffields)#">
                  <cfset fname = ffields[x]>
                      <cfif trim(form[fname]) NEQ "">
                          Do and insert
                      </cfif>
                  </cfloop>

               

              Secondly, I hope you do realize you've assumed that all the fields you've mentioned will be present in the submitted form. Otherwise, you will get an error.

               

              In fact, if they are, and if you wish to consider every field in the form, then you can simplify the above code to something like

               

              <cfloop collection="#form#" item="field">
                      <cfif trim(form[field]) NEQ "">
                          Do and insert    
                      </cfif>
                  </cfloop>

              • 4. Re: Soo close. Looping over certian form elements to see if they are empty
                insuractive Level 3

                     Assuming that you know what the fieldnames are at design-time, you can save some vertical space (and some maintenance time down the road) by

                storing all your fields as a list instead of an array:

                 

                <cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">

                 

                The following code will loop over your specified fields and allow you to execute code for EACH non-empty fields encountered

                 

                <cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">

                <cfloop list="#variables.sReqFields#" index="sFieldName">

                     <cfif isDefined("FORM.#sFieldName#") AND Trim(FORM[sFieldName]) neq "">

                          FIELD IS NOT EMPTY - DO ADDITIONAL PROCESSING ON FIELD HERE

                     </cfif>

                </cfloop>

                 

                However, in your orignal post you mentioned that you want to check to see if all 11 fields were completed and if so, do your insert.  That would look something like this:

                 

                <cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">

                <cfset variables.bFieldsCompleted = true>

                <cfloop list="#variables.sReqFields#" index="sFieldName">

                     <cfif NOT isDefined("FORM.#sFieldName#") OR Trim(FORM[sFieldName]) eq "">         

                          <cfset variables.bFieldsCompleted = false>

                     </cfif>

                </cfloop>

                <cfif variables.bFieldsCompleted>

                     DO YOUR INSERT HERE.

                </cfif>

                 

                Then if you needed to modify the list, you could add or remove elements fairly easily.