3 Replies Latest reply on Apr 22, 2008 5:55 PM by WestSide

    CFLOOP Problem

    WestSide
      Hi,

      I am having a frustrating time with this loop. My code is in a CFC. I have the business requirement that a user is NOT required to fill in values in the form fields, but if they fill in a value in ONE form field, then the rest of the form fields MUST contain values as well. The form fields are dynamically generated on the front end. Here is my loop:

      My code does not quite work, if the first field in the loop has a value it set a variable "hasValues" equal to true, but what I REALLY want to check is do ALL fields have a value.

      Can anyone assist?

      -Westside


        • 1. CFLOOP Problem
          -==cfSearching==- Level 4
          Count the total number of fields and count the number of those fields that are not empty. Then compare the two values. If the non-empty value is greater than 0 but less than the total number of fields, the user must fill in the rest of the fields.
          • 2. Re: CFLOOP Problem
            -==cfSearching==- Level 4
            -==cfSearching==- wrote:
            Count the total number of fields and count the number of those fields that are empty. Then compare the two values. If the empty value is greater than 0 but less than the total number of fields, the user must fill in the rest of the fields.

            Correction, that should be non-empty fields. Though counting empty fields should probably work as well.

            <cfset form["fieldA"] = "Only one field has a value">
            <cfset form["fieldB"] = "">
            <cfset form["fieldC"] = "">

            <cfset totalFields = 0>
            <cfset totalNonEmpty = 0>
            <cfloop collection="#form#" item="field">
            <cfset totalFields = totalFields + 1>
            <cfif trim(form[field]) NEQ "">
            <cfset totalNonEmpty = totalNonEmpty + 1>
            </cfif>
            </cfloop>

            <cfif totalNonEmpty GT 0 AND totalNonEmpty LT totalFields>
            ERROR: User completed one field, but not all fields
            <cfelse>
            OK: User completed all fields or all fields are empty
            </cfif>
            • 3. Re: CFLOOP Problem
              WestSide Level 1
              Yes, that does shed some light. Thank you.

              -Westside