Last active
February 1, 2019 10:04
-
-
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.
This file contains hidden or 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 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Steve.
This is a nice example, and just what I was looking for.