Yesterday I blogged about how to create a custom date renderer for CFGrid. Today I am going to add to that example how to validate the data entered into the CFGrid before it is sent to the cfc to update the database. The CFGrid (Ext.grid.EditorGrid) object has an event called "validateedit", which fires after a cell is edited, but before the value is set in the record. We will be adding a listener to that event that will validate the data that is being submitted and, if the data is not valid, display an alert message and cancel the changes.

You will see in the code below that I have a function called init which is called via ajaxOnLoad() after the grid is loaded. The init() function calls the setDateRenderer() (which will ensures the date is properly formatted when displayed in the grid), and it calls the addValidator() function I created to add a listener to the "validateedit" event. Now whenever the validateedit event fires, it will call the dataValidator() function. The dataValidator() function checks to see if the field that is being edited needs validation, and then validates the data if necessary. In this example, I added validation for the Expiration Date, and the numeric Sponsor ID for the coupon record. If the data fails validation an alert is thrown and the edit event is canceled(reverting the data back to it's original value).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head>
   <title>CFGird Custom Date Validator</title>
   <!--- import the Ext date package --->
   <cfajaximport tags="cfinput-datefield">
   <!--- create javascript function for rendering dates --->
   <script language="JavaScript" type="text/javascript">
   setDateRenderer = function(){
      mygrid = ColdFusion.Grid.getGridObject('CouponsGrid');
      cm = mygrid.getColumnModel();
      cm.setRenderer(3, Ext.util.Format.dateRenderer('m/d/Y'));
      mygrid.reconfigure(mygrid.getDataSource(),cm);
   }
   dataValidator = function(editEvent){
      //Date Field Validation
      if (editEvent.field == "EXPIRATIONDATE"){
         if (isNaN(Date.parse(editEvent.value))){
            alert("Please enter a date in this field.");
            editEvent.cancel = true;
         }
         else {
            data = new Date(Date.parse(editEvent.value));
            editEvent.value = data.dateFormat("m/d/Y");
         }
      }
      //Numeric Field Validation
      if (editEvent.field == "SPONSORID"){
         if (isNaN(editEvent.value)){
            alert("Please enter a numeric value in this field.");
            editEvent.cancel = true;
         }
      }
   }
   addValidator = function(){
      mygrid = ColdFusion.Grid.getGridObject('CouponsGrid');
      mygrid.on('validateedit',dataValidator);
   }
   init = function(){
      setDateRenderer();
      addValidator();
   }
   </script>
</head>

<body>
<!--- Set up the Grid --->
<cfform id="CouponForm" name="CouponForm">
<cfgrid name="CouponsGrid"
format="html"
pagesize="10"
striperows="yes"
selectmode="edit"
bind="cfc:coupons.getCoupons({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})"
onchange="cfc:coupons.editCoupon({cfgridaction},{cfgridrow},{cfgridchanged})">

<cfgridcolumn name="Couponid" display="false" />
<cfgridcolumn name="SPONSORID" header="Sponsor" width="100"/>
<cfgridcolumn name="COUPON" header="Coupon" width="100"/>
<cfgridcolumn name="EXPIRATIONDATE" header="Exp Date" width="200"/>
</cfgrid>
</cfform>

<!--- use AjaxOnLoad to call the init() function --->
<cfset ajaxOnLoad("init")>
</body>
</html>

Click Here to See a Working Example

Comments
Samer Salameh's Gravatar Thank you for the effort.

I tried the example locally but it didn't work as required, also the working example is not working.
Can you help.

Thanks.
# Posted By Samer Salameh | 3/11/08 11:00 AM
Scott Bennett's Gravatar Sorry about that, It worked when I wrote the article but when I moved to my new hosting provider, I never updated the database connection info for this template. The working example should work properly now.
# Posted By Scott Bennett | 3/11/08 1:49 PM
 
Home | Blog | Portfolio | Contact | © 2001 - 2007 The ColdFusion Guy - Scott Bennett. All rights reserved.
BlogCFC was created by Raymond Camden. This blog is running version 5.9.