Ask a Jedi: Mixing cfform validation and ajax-based functions

Andy asks:

If you have an ajax/html form and you submit using a button control which calls a function (like ColdFusion.Ajax.submitForm) is there still a way to easily take advantage of the built in cfform validation? This usually gets called when you use a submit, rather than a button, and I'd love to be able to call it right before I call my function that handles the cfform submission.

If I read you right - you want to know if you can use cfform validation as well as using your own logic after the validation. If so, it works pretty easily:

<cfif not structIsEmpty(form)> <cfdump var="#form#" label="Form"> </cfif>

<script> function doSomething() { alert(‘Called’); return false; } </script>

<cfform name=”test” onSubmit=”return doSomething()”> Who is the twelfth cylon? <cfinput name=”name” required=”true”> <cfinput name=”submit” type=”submit” value=”Submit”> </cfform> </code>

So read this from the bottom up. I’ve got a super simple form using CFFORM’s built in validation for a grand total of one field (I’m easy like Sunday morning). Note the onSubmit. What’s cool, and what I wasn’t sure would work, is that doSomething won’t run if there are any errors. As soon as your form is ok, it runs.

I’m very much a anti-cfform kind of guy, so I may be missing something obvious, but I hope this answers your question.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments