Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active February 1, 2019 10:04
Show Gist options
  • Save stevewithington/6469605 to your computer and use it in GitHub Desktop.
Save stevewithington/6469605 to your computer and use it in GitHub Desktop.
Mura CMS : Example of how to intercept a contentBean before it is saved, and set error(s) if needed.
<cfscript>
// Drop this function in your Site, Theme, or Plugin's eventHandler.cfc
public any function onBeforeContentSave($) {
// reference to the newBean
var newBean = arguments.$.event('newBean');
// reference to the original contentBean
var oldBean = arguments.$.event('contentBean');
// 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(newBean.getValue('summary')) ) {
StructAppend(errors, {'error1': 'Summary 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 ) {
arguments.$.event('contentBean').getErrors()[error] = errors[error];
}
} else {
// no errors, so we're good!
// you can also optionally set any attributes of the contentBean
//newBean.setBody('<h1>No errors</h1>');
}
}
</cfscript>
@visomar
Copy link

visomar commented Aug 12, 2015

Thanks Steve.
This is a nice example, and just what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment