coldfusioncfgridcfgridcolumn

Coldfusion: Simple cfgridupdate validation


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 

Solution

  • 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>