Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronnieduke/9041900 to your computer and use it in GitHub Desktop.
Save ronnieduke/9041900 to your computer and use it in GitHub Desktop.
<cfscript>
// Drop this in your Site or Theme eventHandler.cfc
public any function onBeforeFormSubmitSave($) {
// reference to the formBean
var formBean = arguments.$.event('formBean');
// 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('formBean');
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 &raquo;</a></p>');
}
}
return str;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment