Copy link to clipboard
Copied
I know how you can check for a defined variable or object using isDefined("myVar"), but I have a struct that can have varying properties. While I can explicitly write out the "myStruct.myProperty" checks in quotes for this method, I'd like to put these into a function where a variable is passed into it.
Is this possible? Basically, I'm populating form fields and some fields may not be defined yet. Thoughts? Thanks.
Ryan
Copy link to clipboard
Copied
<cfif StructKeyExists(Form, "your_field_name")> ....
Mack
Copy link to clipboard
Copied
thanks, but it still involves putting the property in quotes. Is there a way to check if myObj.someDynamicProperty exists without having to put it in quotes? Thanks.
Copy link to clipboard
Copied
<cfif StructKeyExists(Form, your_dynamic_property_name)> ....
?
Mack
Copy link to clipboard
Copied
I share Mack's confusion....
It is a basic programming concept that anywhere one can put a string, i.e. "myProperty" one can put a variable that contains a string.
Copy link to clipboard
Copied
I think you are either looking for the #structKeyExists(myStruct,"myProperty")# or arrayNotation i.e. myStruct["myProperty"] which of course can be a varaible.
<cfset node = "myProperty">
<cfoutput>#myStruct[node]#<cfoutput>
Copy link to clipboard
Copied
Not sure if these are it.
I'm populating form fields using a struct object that is created from a query. My goal is when outputting the values into each element, I'm passing myObject.myProperty (no quotes, but the actual object.property) into a function, check that it exists, and then write the value.
How can I go about this? Can I convert the name of the property to a string in order for me to test? It seems that these functions rely on getting a string name to test and can't test on the actual variable. Thanks again.
Copy link to clipboard
Copied
If you are passing myObject.myProperty into a function inside the function is too late to test that myProperty exists in myObject. If it does not exist the call to the function is going to fail. Are you really wanting to test if myOjbect.myProperty is "empty" or are you really trying to test if myObject does or does not have a property named "myProperty"?
Maybe a bit of code that you have tried to use to achieve your goal would clarify what you are really trying to do.
Copy link to clipboard
Copied
This, "I'm populating form fields using a struct object that is created from a query", looks like it might be more complicated than necessary. Given that populating form fields from queries is reasonably straighforward, what added value does your structure give you?
Copy link to clipboard
Copied
My goal is when outputting the values into each element, I'm passing myObject.myProperty (no quotes, but the actual object.property) into a function, check that it exists, and then write the value.
Aha, there, is one source of confusion. The variable myStruct.myProperty may or may not exist. Whereas, myObject.myProperty always exists, and so there is no reason to check for its existence. The usual thing to do is to check for the existence of myStruct.myProperty before passing it as a parameter of a function.
Copy link to clipboard
Copied
Thanks for the help BTW. I'm still new to CF, I'm more of a .net guy.
Basically, I have this. It's a form that edits and adds bios for members:
objStatement = objConnection.PrepareStatement("select bio_first_name, bio_last_name from cpppeBios where bio_id = ?");
objStatement.setInt(1, Int(FORM.edit_bio_id));
objResults = CreateObject("java", "coldfusion.sql.QueryTable").Init(objStatement.ExecuteQuery());
EditBio = structNew();
EditBio = objResults;
....and, I have input fields down below...
<input type='text' value='' />
I use the same form for adding and editing bios. What is the best approach for handling populating of these fields when the EditBio object can be null or contain data. I would access the data using EditBio.bio_first_name, etc.
Thanks again.
Copy link to clipboard
Copied
To find infomation on specfic cold fusion tags, google "<cfNameOfTag> x". For cold fusion functions, google "coldfusion functionname x". In both cases x is the version number and is optional. Invariably the 1st result will be the page of the cfml reference manual you want.
The cold fusion tag that will simplify what you are attempting is <cfquery>