Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

How to validate an array element?

Question posted by: dmorand (Familiar Sight) on March 12th, 2008 05:27 PM
I'm trying to validate whether or not an array element has been created.

Here is what one of the arrays could look like:

commentid[1] = undefined
commentid[2] = undefined
commentid[3] = 1
commentid[4] = 1
commentid[5] = undefined
commentid[6] = 1

I want to check if a certain array element is set to 1, but I'm getting errors in the code below because commentID[1] is undefined. How can I have the code not evaluate if it's undefined. I've tried using isDefined() and isValid() as well.

Code: ( text )
  1. <cfoutput query="getComments">
  2.         <cfif #commentList[commentID]# eq 1>
  3.                 Valid!!!!
  4.         <cfelse>
  5.             not valid!!!
  6.     </cfif>
  7. </cfoutput>
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
acoder's Avatar
acoder
Site Moderator
10,420 Posts
March 12th, 2008
07:16 PM
#2

Re: How to validate an array element?
isDefined() doesn't work with array elements. Set a variable to the array element and then test that instead.

Reply
dmorand's Avatar
dmorand
Familiar Sight
192 Posts
March 20th, 2008
03:53 PM
#3

Re: How to validate an array element?
I'm not able to set a variable to the array element if it's undefined. I tried it and I'm still getting CF errors. I think I'm just not thinking of the best way to handle this, but I'm stumped.

This is what I'm trying to accomplish:
I have a form with a section with a bunch of checkbox fields.
I'm retrieving the fields which were selected using a query. I know which numbers were checked, but now I want to have those fields checked off when the user goes into the form to edit their records.

I just need to either see if the query returned that specific value or not, and either check or not check the box.

Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
March 20th, 2008
05:19 PM
#4

Re: How to validate an array element?
Then why not just use a list and loop over it checking the checkboxes which have a number in the list? Create the list (a string of numbers delimited by commas) using the query. To determine whether to check a checkbox or not, you could use listfind.

Reply
dmorand's Avatar
dmorand
Familiar Sight
192 Posts
March 20th, 2008
06:57 PM
#5

Re: How to validate an array element?
Quote:
Originally Posted by acoder
Then why not just use a list and loop over it checking the checkboxes which have a number in the list? Create the list (a string of numbers delimited by commas) using the query. To determine whether to check a checkbox or not, you could use listfind.


I'll give that a shot. It's so easy in my head, I just cant get the sql/cf code down right.

I'm too used to coding in proprietary systems....I need to get better acquainted with sql/cf.

Reply
acoder's Avatar
acoder
Site Moderator
10,420 Posts
March 20th, 2008
09:02 PM
#6

Re: How to validate an array element?
If you get stuck, post your attempted code, and I can suggest changes.

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,103 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Coldfusion Forum Contributors