Question

Here is my cfselect:

<cfselect
    name="UrgencyId"
    query="GetUrgencies"
    value="id"
    display="Urgency"
    selected="#issue.UrgencyId#">
</cfselect>

It works well except I do not know how to make it disabled according to variable value.

If I do so, it appears disabled:

<cfselect
    name="UrgencyId"
    query="GetUrgencies"
    value="id"
    display="Urgency"
    selected="#issue.UrgencyId#"
    disabled="disabled">
</cfselect>

But when I try to add cfif, an error occurs (ok, I see that layout is wrong):

<cfselect
    name="UrgencyId"
    query="GetUrgencies"
    value="id"
    display="Urgency"
    selected="#issue.UrgencyId#"
    <cfif true>disabled="disabled"</cfif>>
</cfselect>

Ok, I try to use a variable:

<cfset dsbl="disabled='disabled'">
<cfselect
    name="UrgencyId"
    query="GetUrgencies"
    value="id"
    display="Urgency"
    selected="#issue.UrgencyId#"
    #dsbl#>
</cfselect>

and get an error:

Invalid CFML construct found on line 47 at column 139. 
 ColdFusion was looking at the following text:
#

I have read that cfselect has an enabled attribute which acts opposite to disabled HTML attribute. So I tried this:

<cfselect enabled="no">
<cfselect enabled="false">
<cfselect enabled=no>
<cfselect enabled=false>

all this didn't disable my dropdown.

Was it helpful?

Solution

I would highly recommend not using cfform because it tends to do a lot more harm than good, with that being said you can use an attributeCollection to get this working.

<cfset stSelect = {
    name = 'UrgencyId',
    query = 'GetUrgencies',
    value = 'id',
    display = 'Urgency'
} />
<cfif boxShouldBeDisabled>
  <cfset stSelect.disabled = 'disabled' />
</cfif>

<cfselect attributeCollection="#stSelect#">
</cfselect>

You don't specify your ColdFusion version, but the above code should work on CF8 or greater and was tested on CF10.

Not using cfform your code would look like. It's definitely not as clean looking, but it enables you to not use cfform

<select name="UrgencyId">
  <cfloop query="GetUrgencies">
    <option value="#GetUrgencies.id#"<cfif issue.UrgencyID EQ GetUrgencies.id> selected="selected"</cfif><cfif shouldbeDisabled> disabled="disabled"</cfif>>#GetUrgencies.Urgency#</option>
  </cfloop>
</select>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top