Coldfusion: Simple cfgridupdate validation

923 Views Asked by At

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 
2

There are 2 best solutions below

0
On BEST ANSWER

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>
0
On

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>