Last active
September 12, 2019 20:44
-
-
Save stevewithington/7936445 to your computer and use it in GitHub Desktop.
Mura CMS : This is an example of how to perform server side validation of Mura CMS form submissions and display an error message to the end user. This leverages the onBeforeFormSubmitSave and the onFormSubmitErrorRender (as of 6.1) methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Drop this in your Site or Theme eventHandler.cfc | |
public any function onBeforeFormSubmitSave($) { | |
// reference to the formBean | |
var formBean = arguments.$.event('formDataBean'); | |
// example on how to create some errors | |
var error = ''; | |
var errors = {}; | |
// Check for any required fields/attributes, and create an error if missing | |
if ( !Len($.event('message')) ) { | |
StructAppend(errors, {'error1': 'Message is required.'}); | |
} | |
// if ( !Len(newBean.getValue('someExtendedAttribute'))) { | |
// StructAppend(errors, {'error2': 'Some Extended Attribute is required.'}); | |
// } | |
// if there's errors | |
if ( !StructIsEmpty(errors) ) { | |
for ( error in errors ) { | |
formBean.getErrors()[error] = errors[error]; | |
} | |
arguments.$.event('acceptdata', false); | |
} else { | |
// no errors, so we're good! | |
// do whatever else you want to do here (e.g. log something, etc.) | |
} | |
} | |
public any function onFormSubmitErrorRender($) { | |
var str = ''; | |
var formBean = arguments.$.event('formDataBean'); | |
var error = ''; | |
var errors = {}; | |
savecontent variable='str' { | |
if ( !StructIsEmpty(formBean.getErrors()) ) { | |
WriteOutput('<div class="alert alert-danger"><p><strong>Please review the following error(s)</strong></p><ul>'); | |
errors = formBean.getErrors(); | |
for ( error in errors ) { | |
WriteOutput('<li>' & errors[error] & '</li>'); | |
} | |
WriteOutput('</ul></div><p><a href="javascript:history.go(-1);">Try again »</a></p>'); | |
} | |
} | |
return str; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@vipinunnithan & @Luke-Wilson, I've updated the Gist to use
formDataBean
vs.formBean
. Thanks!