3

I trying to use a very simple CFGRID (with CFGRIDUPDATE) but not allow NULLS.

<cfset strWarning= "">  

<cfif IsDefined("FORM.gridEntered")> 
    <cfif FORM.myName EQ '' OR FORM.myURL EQ ''>
        <cfset strWarning= "Error: Form fields cannot be blank">
    <cfelse>
        <cfgridupdate grid="gridSomething" 
          dataSource="qryTSQL" tableName="tblName" keyOnly="Yes"> 
    </cfif> 
</cfif> 

<cfoutput>#strWarning#</cfoutput><br />

<cfform> 
<cfgrid name="gridSomething" query="qryTSQL" 
       selectMode="Edit" format="HTML">  
    <cfgridcolumn name = "myID" display="No"> 
    <cfgridcolumn name = "myName">
    <cfgridcolumn name = "myURL"> 
</cfgrid> 

<cfinput type="submit" name="gridEntered"> 

The error I am getting is - Error Diagnostics: Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.

I think I am under thinking this desired solution.

Thanks.

EDIT: Updated Error Message

Error Diagnostics: Element MYURL is undefined in FROM.

Form scope - struct

FIELDNAMES  GRIDENTERED,__CFGRID__CFFORM_1__GRIDSOMETHING
GRIDENTERED Submit Query
GRIDSOMETHING.MYID  Form scope - array 
1   1001 

GRIDSOMETHING.MYURL Form scope - array 
1   /test_d.cfm 

GRIDSOMETHING.ORIGINAL.MYID Form scope - array 
1   1001 

GRIDSOMETHING.ORIGINAL.MYURL    Form scope - array 
1   /changed.cfm 

GRIDSOMETHING.ROWSTATUS.ACTION  Form scope - array 
1   U 

__CFGRID__CFFORM_1__GRIDSOMETHING   __CFGRID__EDIT__=2 MYID Y MYURL Y 1 U 1001 1001 /test_d.cfm /changed.cfm 
nope_four
  • 492
  • 1
  • 4
  • 16
  • 1
    The key is to find out what variable is causing the error. I suggest commenting out everything between the cfset and cfoutput tags and then re-running the page. If you don't get an error, start uncommenting things bit by bit to help identify that variable. – Dan Bracuk Dec 27 '12 at 17:02
  • Can you add the complete error text (ie: copy and paste it from the screen, and ensure you have "Robust Exception Handling" enabled in CFAdmin on your dev environment). It should highlight the exact line that is causing the problem. I cannot see anything in the code you posted which would yield that error though. – Adam Cameron Dec 27 '12 at 17:10

2 Answers2

3

Grids work differently than standard FORM fields. Since grids contain multiples rows of data, CF creates arrays of the new/changed values. One for each grid column ie

     FORM.gridname.columnName

To determine if any of the values for a specific column are empty, you need to loop through the array for that column and check each value:

    <cfset updateCount = arrayLen(FORM.gridSomething.myName) />
    <cfloop from="1" to="#updateCount#" index="x">
         <cfset nameValue = trim( FORM.gridSomething.myName[x] ) />
         <cfset urlValue  = trim( FORM.gridSomething.myURL[x]) ) />

         <!--- one or both of the values is empty --->
         <cfif not len( nameValue ) OR not len( urlValue )>
             ... empty value found. do something here ....

         </cfif>
    </cfloop>
Leigh
  • 28,765
  • 10
  • 55
  • 103
0

Add this function to you code and call it passing it the FORM scope as follows; On page or CFC works the same! Clears up errors with some versions of CF that choke on blank cols in a simple grid update

    <cffunction name="cleanGridUpdate" >
        <cfargument name="FORM" >
        <cfloop collection="#FORM#" item="thisOne">
        <cftry>
            <cfset thisDeep = arrayLen(FORM[thisOne])>
                <cfloop index="x" from="1" to="#thisDeep#">
                    <cfif len(FORM[thisOne][x])lt 1>
                        <cfset FORM[thisOne][x] = javaCast( "null", 0 )>
                    </cfif>
                </cfloop>               
            <cfcatch>
                <cfset cat=1>   
            </cfcatch>
          </cftry>
          </cfloop>
         <cfreturn form>
     </cffunction>

    <cfif isDefined('URL.action') AND URL.action eq 'gridUpdate'>
       <cfset cleanGridUpdate(FORM)>
       <cfgridupdate grid="#URL.table#" table="#URL.table#" datasource="your_DS" 
        keyonly="yes" > 
    </cfif>


    <cfform action="##?action=gridUpdate&table=cheesePuffs" method="post">
       <cfgrid name='cheesePuffs' format='HTML'/>
       <cfinput name="submit" type="submit" value>
    </cfform>
Uncle Dave
  • 1
  • 1
  • 2