1 Reply Latest reply: Sep 15, 2007 3:19 PM by itisdesign RSS

    isValid("string" vs <cfparam type="string"

    itisdesign MVP
      Just a quick note regarding a difference between isValid and <cfparam, to hopefully help others down the line...
      (pay attention to line 5 - it is the difference between the two)

      Example #1:

      <cfset foo.test = "" />
      <cfset bar = queryNew("test") />
      <cfset queryAddRow(bar, 1) />
      <cfset querySetCell(bar, "test", "bla") />
      <cfset foo.test = bar['test'] />
      <cfoutput>#isValid("string", foo.test)#</cfoutput>
      <cfparam name="foo.test" type="string" />

      In example #1, the isValid("string", ...) function does not throw an error, but the <cfparam type="string"... does.

      Example #2:

      <cfset foo.test = "" />
      <cfset bar = queryNew("test") />
      <cfset queryAddRow(bar, 1) />
      <cfset querySetCell(bar, "test", "bla") />
      <cfset foo.test = bar['test'][1] />
      <cfoutput>#isValid("string", foo.test)#</cfoutput>
      <cfparam name="foo.test" type="string" />

      In example #2, no error is thrown.

      As I mentioned, the difference is...
      Line #5 in 1st example: <cfset foo.test = bar['test'] />
      Line #5 in 2nd example: <cfset foo.test = bar['test'][1] />

      If you do not specify the query row number, when accessing a query variable using array notation, isValid() may not throw an error, however <cfparam> might throw an error.

      Just an interesting observation.

      -Aaron Neff
        • 1. Re: isValid(&quot;string&quot; vs &lt;cfparam type=&quot;string&quot;
          itisdesign MVP
          To clarify... If the query row is omitted from the indexed/array notation, then isValid() and <cfparam /> produce different results.

          In the attached code example below, the query row was omitted intentionally to illustrate.

          Two possible solutions are:

          1) Make isValid() throw an error (so that they both throw an error)
          2) Make <cfparam /> not throw an error (so that they both do not throw an error)

          I feel the behavior should be consistent. Does anyone feel otherwise? If the behavior should be consistent, then which solution, or other solution, is preferred?

          Let's discuss!

          Thanks,
          -Aaron
          ---------