3
votes

I am trying to manually set a variable INSIDE a form, because it contains html and placing it in the value attribute of a tag would cause errors in the display. Currently, I check to see if that attribute contained html, and if so, the field is empty.

I'd like to be able to set the variable as the old value if it contained html in previous entries of the form, so that the user wouldn't have to enter that field every time they loaded that ORM object to edit.

Here's a snippet:

<cfif ("#dataobject.getField()#" NEQ "" AND Left(dataobject.getField(), 1) EQ "<")>
<cfscript>
    temp = dataobject.getField();
    temp2=temp;
    temp2 = Insert("---", temp2, 0);
    temp2 = Insert("<!", temp2, 0);
    temp2 = Insert("--->", temp2, Len(temp2));
    dataobject.setField(temp2);                             
</cfscript>

<label for="name">
    Field:
</label>
<input type="text" name="Field" value="">
<button id="savefield" name="savefield">Save</button>
<cfif form.Field EQ ""><cfset form.Field = temp></cfif>
<cfscript>
    dataobject.setField(temp);
</cfscript>

<cfelse>
<label for="name">
    Field:
</label>
<input type="text" name="Field" <cfif ("#dataobject.getField()#" NEQ "")>value="#dataobject.getField()#"</cfif>>
<button id="savefield" name="savefield">Save</button>
</cfif>

The code I was trying to use: <cfif form.Field EQ ""><cfset form.Field = temp></cfif>

Coldfusion throws an error saying that the FORM variable is undefined (which doesn't surprise me). The "savefield" button calls javascript that opens a window allowing the user to set the value, then closes. Should I put my code there instead?

-The inserts which turn the string into a comment was an early attempt at a workaround that didn't work :/

3

3 Answers

3
votes

Ok hopefully the following pointers will help you going:

  • Verify that the field exists in the form via; structKeyExists(form, "field") or use cfparam to initialize a default value
  • Escape the value with HTMLEditFormat(dataObject.getField()) to escape any HTML code which breaks the html

So e.g.;

<cfparam name="form.field" default="" />

<label for="field">
    Field:
</label>
<input type="text" id="field" name="field" value="#htmlEditFormat(form.field)#" />
<button id="savefield" name="savefield">Save</button>

Gl !

2
votes

if you want to force a value into the form (or any) scope you'll want to use cfparam before you use it.

<cfparam name = "form.field" default = "">
<cfif form.Field EQ "">
  <cfset form.Field = temp>
</cfif>

Essentially this is the same thing as

<cfif !structKeyExists(form,"field")>
  <cfset form.field = "">
</cfif>
0
votes

Don't forget that preservedata is your friend. You don't have to worry about the value="" attribute if you populate the form field ahead of time.

<cfquery name="qry">
SELECT Field1,Field2
FROM table
WHERE ID=<cfqueryparam cfsqltype="cf_sql_integer" value="#url.ID#">
</cfquery>

<cfloop list="#qry.Columnlist#" index="FieldName">
    <cfset form[FieldName] = HTMLEditFormat(qry[FieldName][1])>
</cfloop>

<cfform preservedata="yes">
    <label for="Field1">Field One:</label>
    <cfinput name="Field1">
    <label for="Field2">Field Two:</label>
    <cfinput name="Field2">
</cfform>