From: WestSide on
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




<cfset hasValues = false />

<!--- Do the fields have any values, if so, then they all are required --->
<cfloop list="#gradeList#" index="grade">
<cfloop collection="#arguments#" item="field">
<cfif field CONTAINS 'list_' & grade>
<!--- If ANY of the fields have values --->
<cfif arguments[field] neq "">
<cfset hasValues = true />
<cfbreak />
</cfif>
</cfif>
</cfloop>
</cfloop>

From: -==cfSearching==- on
Count the total number of fields [i]and[/i] count the number of those fields
that are empty. Then compare the two values. If the empty values is greater
than 0 but less than the total number of fields, the user must fill in the rest
of the fields.

From: -==cfSearching==- on
-==cfSearching==- wrote:
[i]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.[/i]

Correction, that should be [b]non-empty[/b] 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>


From: WestSide on
Yes, that does shed some light. Thank you.

-Westside