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.

[More]

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
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
JC's Gravatar I have tried the example given to validate data and receive the following Firebug error:
   
init is not defined
ColdFusion.Event.registerOnLoad(init,null,false,true);

I am new to CF8 and the EXT framework and am not sure what the issue is. Any help is greatly appreciated.
# Posted By JC | 1/13/09 12:52 PM
Scott Bennett's Gravatar @JC,

Make sure that the <script> block that contains your init() javascript function is in within the <head> and </head> tags. The ext stuff can be picky if you don't define all your javascript functions in the head of the html.

-Scott
# Posted By Scott Bennett | 1/13/09 12:59 PM
Sandy's Gravatar Is there a way using the cfgrig 8 with format="html" to be able to
have a ADD button that would add a record to the datagrid which is
is binded to a coldfusion component and then record would be
inserted in the database?
Is there a sample you know of for that?
# Posted By Sandy | 1/22/09 7:24 PM
Tones411's Gravatar Yes, make sure the javascript init() is in the head area.

Here's a trick to get it into the head area if you are using some kind of template system:

<cfsavecontent variable="javascript">
<script type="text/javascript">
function init(){
...
}
</script>
</cfsavecontent>
<cfhtmlhead text="#javascript#">

<cfset ajaxOnLoad("init")>
# Posted By Tones411 | 2/18/09 1:08 PM
khowell's Gravatar I have tried to incorporate this into an application that I am working with, but to no success. I keep getting a javascript error when the page loads. ('init' is undefined). In our environment, the head tag
is open and closed through an index page when the page loads, so I was using the cfhtmlhead tag to throw the init function into the <head> tags. Just can't seem to get it working, any thoughts?
# Posted By khowell | 4/20/09 6:42 PM
Bob Chesley's Gravatar I also ran into this and you can simply call the CF function directly in your page rather than using the ajaxonload function approach.

For example:
<script type="text/javascript">
ColdFusion.Event.registerOnLoad(init,null,false,true);
</script>
# Posted By Bob Chesley | 7/24/09 4:49 PM
S.C. Adams's Gravatar I am trying this in IE 7. And when it 'resets' the date it subtracts
one day. So 10/1/2009 becomes 11/30/2009. Works in
FF.
And I have no control over what browser my user uses.
# Posted By S.C. Adams | 12/9/09 11:39 AM
S.C. Adams's Gravatar I found a bug when working with the date validation part
It was not displaying the error message or returing the correct
original date.

I changed the code to this for the date validation.
if (isNaN(Date.parse(editEvent.value))){
alert("Please enter a date in this field.");
editEvent.cancel = true;
data = new Date(Date.parse(editEvent.originalValue));
}
else {
data = new Date(Date.parse(editEvent.value));
}
editEvent.value = data.dateFormat("m/d/Y");

ANd now it works like a charm.
# Posted By S.C. Adams | 12/16/09 6:30 PM
Jochen's Gravatar This example was exactly what I needed. But since CF 9 it is no longer running. No validation since the update of server. Any guess?
# Posted By Jochen | 4/13/10 10:11 AM
Scott Bennett's Gravatar @Jochen,

In ColdFusion 9 they changed to a new version of the underlying EXT framework. As a result most of the workarounds like this that I did in CF8 will not work. For this example I was making a custom date validator, because that was not built into CF8. However in CF9 they added some new options for the 'type' attribute in the 'cfgridcolumn' tag and now you can just use that to make sure the dates in a cfgrid are valid.

<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" type="date" width="200"/>
</cfgrid>
</cfform>

One thing to note however is that you in your cfc that is bound to the grid, you will need to add a function to convert the date string returned by the grid to a ColdFusion datetime value.

<cfscript>
function ISODateToTS(str) {
return ParseDateTime(ReplaceNoCase(left(str,16),"T"," ","ALL"));
}
</cfscript>

   <cfquery datasource="#THIS.dsn#">
   UPDATE COUPONS
   SET <cfswitch expression="#colname#">
    <cfcase value="EXPIRATIONDATE">#colname# = #createodbcdatetime(ISODateToTS(value))#</cfcase>
    <cfdefaultcase>#colname# = '#value#'</cfdefaultcase>
    </cfswitch>
   
   WHERE COUPONID = #ARGUMENTS.gridrow.COUPONID#
   </cfquery>
# Posted By Scott Bennett | 4/14/10 12:59 PM
Matt's Gravatar The dataValidator function part of this has been very helpful.
I'm using CF9 and I have a couple of cfgrids of html format in my app.

This code is useful for when a user has just changed the content of a cell.
But what about when they insert a new row (insert=yes) and the values are null?

If they submit the form, records w/null values will get submitted (I've checked, it works.)
I'd like to cancel the submit and reload the form page so it re-displays w/out the new insert rows (w/a little 'alert' as to why, of course).

But this is really throwing me for a loop
# Posted By Matt | 4/21/10 6:56 PM
https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Clapton-PM-Bag-N44243-Magnolia-2361-it.html https://www.bababorses.de/Louis-Vuitton-LV-Trainer-Men-s-Sneakers-Top-Quality-15-5322-it.html https://www.bababorses.de/Celine-Small-Cabas-Bag-In-Black-Leather-it-2087 https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Call-Back-Sandals-Nude-6162-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-MM-Monogram-Vernis-Leather-In-Magenta-4069-it.html https://www.bababorses.de/Louis-Vuitton-Ring-09-557-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-LV-Square-Espadrilles-Slipper-Sandals-Brown-6371-it.html https://www.bababorses.de/Prada-Golden-Saffiano-Calfskin-Leather-Top-Handle-Bag-it-2956 https://www.bababorses.de/Dior-Diorissimo-Small-Bag-Black-Nappa-Leather-Silvery-Hardware-8001-it-22 https://www.bababorses.de/Louis-Vuitton-Idylle-Blossom-Charms-Necklace-Q94360-406-it.html https://www.bababorses.de/Louis-Vuitton-Color-Blossom-BB-Star-Pendant-Necklace-Red-Gold-309-it.html https://www.bababorses.de/Bvlgari-Serpenti-Original-Leather-Framed-Pochette-Sky-Blue-82121-it-1938 https://www.bababorses.de/Louis-Vuitton-Horizon-55-Trolley-Travel-Luggage-Bag-Taiga-Leather-M30331-Red-6892-it.html https://www.bababorses.de/Fendi-By-The-Way-Small-Croc-Satchel-White-it-2731 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-and-PVC-Nano-Bag-M61114-3176-it.html https://www.bababorses.de/Louis-Vuitton-Sarah-Multicartes-Wallet-M61273-Hot-Pink-7624-it.html https://www.bababorses.de/louis-vuitton-speedy-30--Damier-Azur-Canvas-n44367-2300-it.html https://www.bababorses.de/Hermes-Birkin-35cm-cattle-skin-vein-Handbags-blue-golden-it-907 https://www.bababorses.de/Saint-Laurent-Baby-Sac-De-Jour-Bag-In-Rose-Grained-Leather-it-3322 https://www.bababorses.de/Louis-Vuitton-Twist-MM-M53531-M53532-2775-it.html https://www.bababorses.de/Louis-Vuitton-Sunglasses-133-978-it.html https://www.bababorses.de/Louis-Vuitton-Neverfull-MM-M54185-Black-2705-it.html https://www.bababorses.de/Prada-Saffiano-East-West-Medium-Tote-Bag-Nero-it-3042 https://www.bababorses.de/Louis-Vuitton-Compact-Wallet-in-Monogram-Canvas-M63041-7399-it.html https://www.bababorses.de/Prada-Mens-Leather-Pouch-3312-Black-it-3099 https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Lock-It-Flat-Mule-1A7TOX-Pink-5965-it.html https://www.bababorses.de/Fendi-Baguette-Micro-Monster-Bag-Purple-Multi-it-533 https://www.bababorses.de/Louis-Vuitton-LV-Angel-Stud-Earrings-M64293-435-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Zippy-Wallet-Evasion-M61360-7219-it.html https://www.bababorses.de/LOUIS-VUITTON-CATOGRAM-SQUARE-SCARF-MP2266-4818-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Triangle-Shaped-Messenger-Bag-M54330-Black-3865-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Chloe-Marcie-Medium-Satchel-Bag-Cobalt-it-2283 https://www.bababorses.de/louis-vuitton-epi-leather-Soufflot-BB-bag-m55613-black-2580-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55735-4512-it.html https://www.bababorses.de/Louis-Vuitton-Crafty-NeoNoe-MM-bag-black-M45497-2980-it.html https://www.bababorses.de/Louis-Vuitton-Men-Box-Bag-Shoulder-Body-Bag-M44157-Brown-3136-it.html https://www.bababorses.de/Prada-Saffiano-Double-Zip-Executive-Tote-Bag-Gray-it-3025 https://www.bababorses.de/Louis-Vuitton-Epi-Leather-NeoNoe-BB-Bucket-Bag-M53610-Indigo-2564-it.html https://www.bababorses.de/Saint-Laurent-Small-Monogram-Tassel-Satchel-In-Red-Crocodile-Leather-it-3158 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV18-59-it.html https://www.bababorses.de/LOUIS-VUITTON--CLASSIC-MINI-PACKBACK-2872-it.html https://www.bababorses.de/Balenciaga-Velo-Anthracite-store-it-1723 https://www.bababorses.de/Louis-Vuitton-Monogram-Ebene-Canvas-Pegase-Legere-53-Business-Rolling-Luggage-6950-it.html https://www.bababorses.de/Louis-Vuitton-Crocodilien-Brillant-Capucines-Mini-Bag-N93429-Black-2231-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Hoodie-Jacket-Black-1526-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Coated-Canvas-Popincourt-PM-M43462--Raisin-3345-it.html https://www.bababorses.de/Louis-Vuitton-Lockme-Cabas-Tote-M55028-Black-4530-it.html https://www.bababorses.de/Givenchy-Antigona-Small-Leather-Satchel-Bag-Black-it-2432 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Small-Malle-Chain-Bag-3294-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Zippy-Wallet-M62304-Red-7304-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV113-38-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10.5cm-Eyeline-Pumps-Python-Pattern-Suede-Black-5999-it.html https://www.bababorses.de/LOUIS-VUITTON-PEGASE-LEGERE-REGATTA-N41620-MONOGRAM-CANVAS-6974-it.html https://www.bababorses.de/Louis-Vuitton-Kimono-Wallet-M56175-Pink-7437-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Tapestry-Denim-Bidart-Espadrilles-Blue-5726-it.html https://www.bababorses.de/Louis-Vuitton-Women-s-Escale-Shirtdress-Blue-1709-it.html https://www.bababorses.de/Louis-Vuitton-Montaigne-MM-M41048-Black-4597-it.html https://www.bababorses.de/Louis-Vuitton-Heel-10cm-Crystals-Call-Back-Sandals-Suede-Red-6160-it.html https://www.bababorses.de/Hermes-Bolide-31cm-Togo-Leather-Green-Bag-it-1070 https://www.bababorses.de/Replica-Hermes-Wallet-H001-Wallet-Cow-Leather-Green-it-1558 https://www.bababorses.de/Celine-Medium-Luggage-Tote-Black-Brown-White-Bag-it-2168 https://www.bababorses.de/Louis-Vuitton-Pochette-Voyage-MM-Bag-Damier-Graphite-Canvas-Pixel-N60176-Green-7278-it.html https://www.bababorses.de/Fendi-Black-Snake-Veins-Leather-With-Beige-Ferrari-Leather-Top-handle-Bag-it-469 https://www.bababorses.de/Louis-Vuitton-All-over-Monogram-Sleeveless-Belted-Dress-Navy-1375-it.html https://www.bababorses.de/Louis-Vuitton-Ring-02-560-it.html https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV32-76-it.html https://www.bababorses.de/Louis-Vuitton-Dauphine-MM-M55071-Blue-4511-it.html https://www.bababorses.de/Louis-Vuitton-Supreme-Iphone-Case-White-Red-212-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Smooth-Leather-Twist-Shoulder-Bag-MM-Pink-Black-2639-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Cosmetic-Pouch-Bag-M80502-Bouton-de-Rose-Pink-By-The-Pool-Capsule-Collection-4296-it.html https://www.bababorses.de/Louis-Vuitton-Bracelet-21-271-it.html https://www.bababorses.de/Louis-Vuitton-Heel-9.5-cm-Star-Trail-Ankle-Boots-Black-5511-it.html https://www.bababorses.de/Louis-Vuitton-Geronimos-Belt-Bag-M43502-Black-Epi-Leather-2646-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Ebene-Canvas-Vavin-Chain-Wallet-N60222-Bordeaux-Red-7221-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Empreinte-Leather-Zippy-Coin-Purse-M80408-Cream-Saffron-By-The-Pool-Capsule-Collection-7741-it.html https://www.bababorses.de/Louis-Vuitton-Vintage-Monogram-Vernis-Bleecker-Box-Top-Handle-Bag-Burgundy-4172-it.html https://www.bababorses.de/Prada-Saffiano-Mini-Galleria-Crossbody-Bag-Beige-it-2708 https://www.bababorses.de/Louis-Vuitton-Sunglasses-39-1042-it.html https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Leopard-Print-Onthego-Tote-Bag-M44674-Black-White-3232-it.html https://www.bababorses.de/Louis-Vuitton-Epi-Leather-Twist-PM-Bag-with-Crystal-embellished-Chain-M55412-White-2630-it.html https://www.bababorses.de/Fendi-Chameleon-Red-Cross-Veins-Leather-Tote-Bag-it-488 https://www.bababorses.de/Louis-Vuitton-Monogram-Canvas-Onthego-Tote-Bag-M44571-Kaki-3270-it.html https://www.bababorses.de/Fendi-Earth-Yellow-Leather-with-Multicolor-Striped-Fabric-Shopping-Handbag-it-771 https://www.bababorses.de/Louis-Vuitton-Croco-Pattern-Petite-Boite-Chapeau-Bag-Black-4090-it.html https://www.bababorses.de/Prada-Saffiano-Small-Double-Handle-Tote-Bag-Light-Gray-Pomice-it-2849 https://www.bababorses.de/Louis-Vuitton-Lvxlol-Speedy-BB-M45202-Golden-3125-it.html https://www.bababorses.de/Louis-Vuitton-Gloria-Flat-Open-Back-Loafers-Monogram-Canvas-5798-it.html https://www.bababorses.de/LOUIS-VUITTON-BREA-PM-Monogram-Vernis-leather-IN-MORDORE-4074-it.html https://www.bababorses.de/Replica-Hermes-Steve-H2810-Ladies-Shoulder-Bag-Cow-Leather-it-1428 https://www.bababorses.de/Louis-Vuitton-Noe-bag-M42226-Brown-3496-it.html https://www.bababorses.de/Louis-Vuitton-Damier-Azur-Canvas-I-2260-it.html https://www.bababorses.de/Christian-Dior-Multicolor-PeachYellow-Zipper-Wallet-118-it-223 https://www.bababorses.de/Fendi-By-the-Way-Small-Tricolor-Satchel-Bag-it-2916 https://www.bababorses.de/Prada-Medium-Vitello-Diano-Open-Tote-Bag-Pomice-it-2728 https://www.bababorses.de/Luxury-Hermes-Wallet-H001-Unisex-Wallet-it-1598 https://www.bababorses.de/Louis-Vuitton-Iphone-Case-LV42-104-it.html https://www.bababorses.de/Prada-Saffiano-Small-Gardeners-Tote-Bag-Blue-it-2803 https://www.bababorses.de/Louis-Vuitton-Sac-Tricot-Bag-Epi-Leather-Red-M52805-2736-it.html https://www.bababorses.de/Louis-Vuitton-Crazy-in-Lock-Strass-Bracelet-Silver-316-it.html