Copy link to clipboard
Copied
Hi;
I have an odd problem I can't seem to figure out....I wrote a couple of neat little functions to create boolean selects, populate them with the values from a database and "select" the correct one inthe html form.
<cffunction name="booleanSelect" access="public" output="yes" returntype="string"
description="Creates a booleaen select dropdown" >
<cfargument name="formvar" required="yes" type="string" />
<cfargument name="ffalse" required="yes" type="string" />
<cfargument name="ttrue" required="yes" type="string" /><cfscript>
myvar=evaluate("form.#arguments.formvar#");
if(myvar eq 0){sel = 'selected="selected"';}else{sel = '';}
writeoutput('<option value="0" '&sel&' >'&arguments.ffalse&'</value>');
if(myvar eq 1){sel = 'selected="selected"';}else{sel = '';}
writeoutput('<option value="1" '&sel&' >'&arguments.ttrue&'</value>');
</cfscript></cffunction>
can be called via:
<select name="status" id="status">
<cfscript>booleanSelect('status','Disabled','Enabled');</cfscript>
</select>
and that all works [that's the fixed version]
What I don't understand is why this does not work, the following DOES work in an insert form but not an edit form. and in both cases the form field used to populate the formvar argument is populated and has a value...
<cffunction name="booleanSelect" access="public" output="yes" returntype="string"
description="Creates a booleaen select dropdown" >
<cfargument name="formvar" required="yes" type="string" />
<cfargument name="ffalse" required="yes" type="string" />
<cfargument name="ttrue" required="yes" type="string" />
<cfscript>
if(form[arguments.formvar] eq 0){sel = 'selected="selected"';}else{sel = '';}
writeoutput('<option value="0" '&sel&' >'&ffalse&'</value>');if(form[arguments.formvar] eq 1){sel = 'selected="selected"';}else{sel = '';}
writeoutput('<option value="1" '&sel&' >'&ttrue&'</value>');
</cfscript></cffunction>
basically, why do I have to evaluate the formvar argument in one case and not in the other? I think I might be missing something important here....
-sean
Copy link to clipboard
Copied
I didn't understand your code or what it was trying to do, but I do understand the problem in the subject line. You are trying to use a query, query.column, form, url, or some other structure as a string or number. Look at the line number of your error message to see where it happened. See what variable you were trying to use, then cfdump it before you get to that line.
Copy link to clipboard
Copied
basically I have a table in a database with a bunch of boolean '1','0'columns.
I have a function that reads the database table and sets form variables based on the database column and it's default value for an insert action...
for an update action I have another function that reads the tables data and populates the form variables based on the value in the data column
- works very well, all I have to do is make sure my form fileds are named the same as in the database
so for the drop down boolean options I created a function to create the dropdown and select wother the default value or the column value baesd on whether it is an insert or update action... so:
<select name="status" id="status">
<cfscript>booleanSelect('status','Disabled','Enabled');</cfscript>
</select>
where 'status' is the column name AND the form variable 'form.status' , 'Enabled' & 'Disabled' are just the labels to apply to '1' or '0' on output
at the top of the insert pages I create the default form variables:
fieldsDefaults('_column_name_','_fields_to_ignore_');
which calls:
<cffunction name="fieldsDefaults" access="public" output="yes" returntype="void"
description="creates default form fields froma database query" >
<cfargument name="table" required="yes" type="string" />
<cfargument name="ignorelist" required="no" type="string" default="" />
<cfargument name="column" required="no" type="string" default="false" />
<cfif arguments.column is not "false">
<cfquery name="showcolumns" datasource="#application.dsn#">
select #arguments.column# from #arguments.table#;
</cfquery>
<cfloop query="showcolumns">
<cfparam name="form.#createVar(evaluate(arguments.column))#" default="" />
</cfloop><cfelse>
<cfquery name="showcolumns" datasource="#application.dsn#">
show columns from #arguments.table#;
</cfquery>
<cfloop query="showcolumns">
<cfif ListFindNoCase(field,ignorelist) eq 0>
<cfparam name="form.#field#" default="#default#" />
</cfif>
</cfloop>
</cfif>
</cffunction>
to create the <cfparam> form defaults...
now on to the edit.... calls this:
<cffunction name="dataDefaults" access="public" output="yes" returntype="void"
description="creates default form fields froma database query" >
<cfargument name="table" required="yes" type="string" />
<cfargument name="id" required="yes" type="numeric" />
<cfargument name="ignorelist" required="no" type="string" default="" />
<cfargument name="column" required="no" type="string" default="false" />
<cfquery name="getnames" datasource="#application.dsn#">
show columns from #arguments.table#
</cfquery>
<cfquery name="getdata" datasource="#application.dsn#">
select * from #arguments.table# where id = '#arguments.id#';
</cfquery>
<cfloop query="getnames">
<cfif ListFindNoCase(field,ignorelist) eq 0>
<cfset form[#field#] = "#getdata[getnames.field]#" />
</cfif>
</cfloop>
</cffunction>
which does close to the same thing, but instead populates the form variables with actual database data......
-sean
Copy link to clipboard
Copied
...
<cfargument name="formvar" required="yes" type="string" />
...if(form[arguments.formvar] eq 0){sel = 'selected="selected"';}else{sel = '';}
The existence of a form should be known only to the action page. In other words, the form scope exists within the context of the form's action page. A function is, at best, a freewheeling Samurai. Or it should be. It shouldn't be expected to know details about the caller.
Modify your code so that the caller has to pass the form variable (form.formFieldName) rather than the field name. There are also a number of things wrong with the code.
1) It is almost never necessary to have a function write out output like that. Change the setting to output="no".
2) </value> is apparently a mistake. That should be </option>.
3) The 2 lines
if(myvar eq 0){sel = 'selected="selected"';}else{sel = '';}
if(myvar eq 1){sel = 'selected="selected"';}else{sel = '';}
are identical, but for myVar. It suggests that the value of myVar doesn't actually matter.
Is the following perhaps what you wish to have:
<cffunction name="booleanSelect" access="public" output="no" returntype="string"
description="Creates a booleaen select dropdown" >
<cfargument name="formvar" required="yes" type="string" />
<cfargument name="ffalse" required="yes" type="string" />
<cfargument name="ttrue" required="yes" type="string" />
<cfscript>
var dropDownHTML0 = "";
var dropDownHTML1 = "";
if(formvar eq 0) {
dropDownHTML0 = '<option value="0" selected="selected">' & arguments.ffalse & '</option>' ;
dropDownHTML1 = '<option value="1">' & arguments.ttrue & '</option>' ;
}
if(formvar eq 1) {
dropDownHTML0 = '<option value="0">' & arguments.ffalse & '</option>' ;
dropDownHTML1 = '<option value="1" selected="selected">' & arguments.ttrue & '</option>' ;
}
return dropDownHTML0 & dropDownHTML1;
</cfscript>
</cffunction>
The caller then has to do something like:
<cfscript>booleanSelect(form.status,'Disabled','Enabled');