Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevewithington/7936445 to your computer and use it in GitHub Desktop.
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.
<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 &raquo;</a></p>');
}
}
return str;
}
</cfscript>
@vipinunnithan
Copy link

Hi Steve,
I am new in Mura CMS. I have tried your sample provided for server side form validation.But it was not working for me. It worked only when I changed line number 5 and 33 with this one
var formBean = arguments.$.event('formDataBean')
Could you please confirm I am doing it in the right way?
Regards
Vipin

@Luke-Wilson
Copy link

+1 on Vipin's comment. Once I changed to grab the formDataBean, it worked as expected.

@stevewithington
Copy link
Author

@vipinunnithan & @Luke-Wilson, I've updated the Gist to use formDataBean vs. formBean. Thanks!

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