• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

CFLOOP Problem

Engaged ,
Apr 22, 2008 Apr 22, 2008

Copy link to clipboard

Copied

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


TOPICS
Advanced techniques

Views

257

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Apr 22, 2008 Apr 22, 2008
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.

Votes

Translate

Translate
Valorous Hero ,
Apr 22, 2008 Apr 22, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 22, 2008 Apr 22, 2008

Copy link to clipboard

Copied

-==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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 22, 2008 Apr 22, 2008

Copy link to clipboard

Copied

LATEST
Yes, that does shed some light. Thank you.

-Westside

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation