Kill Missing form variable in Session, and redefine it

coldfusion

The Form variable in my form is being passed/retrieved via Session.

I have a troublesome Form variable(formField1) that was NOT DEFINED properly in the Session, but the whole form was put into use. Then this particular Form variable(formField1) was filled and used, trapping it in the Session and throwing an error. I gave a stop gap solution:

 <cfif isDefined("TTsession.filing.formField1")>
       <cfset TTD formField1 = TTsession.filing.formField1>
     <cfelse>
       <cfset TTD formField1 = "0">
     </cfif>

TTsession= name of the Session variable
formField1= name of the erroneous form field.

As a result of which all the forms in which TTsession.filing.formField1 was improperly defined are giving an output of 0.

I do not want this. What I want to do is:

  1. Check Does form variable EXIST in session?
  2. If yes,
    <cfset FormVar1=session.FormVar1>
  3. If no,
    1. KILL THE MISSING form variable in that Session.
    2. re-Define the form variable FormVar1 right here in that Session.
      <cfset FormVar1=session.FormVar1>

I am using CFMX6.1.

Best Answer

<cfparam name="FormVar1" default="0">

<!--- set overide defaults if in the session --->
<cfif StructKeyExists( TTsession.filing, "formField1")>
   <cfset FormVar1 = TTsession.filing.formField1>
</cfif>

I think the problem might be where you have created this session, however if you merely want to set defaults just use cfparam :

 <cfparam name="session.filling.formfield1" type="numeric" default="0">

So if the session var is defined, do nothing. Else define it with the value of zero. All in one line.

Related Topic