Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cmourizard/5378073 to your computer and use it in GitHub Desktop.
Save cmourizard/5378073 to your computer and use it in GitHub Desktop.
SugarCRM custom form validation with UnderscoreJS
SUGAR.util.doWhen("typeof(check_form) != 'undefined' && typeof check_form == 'function'", function() {
check_form = _.wrap(check_form, function(originalCheckFormFunction, originalCheckFormFunctionArg) {
// Adding custom validation
isCustomValid = confirm('Are you sure you want to save this record?');
if(isCustomValid) {
// If custom validation is positive, calling original Sugar validation
return originalCheckFormFunction(originalCheckFormFunctionArg);
} else {
return false;
}
});
});
<?php
$viewdefs ['Contacts'] = array (
'EditView' => array (
'templateMeta' => array (
'includes' => array (
array (
'file' => 'custom/include/javascript/underscore-min.js',
),
array (
'file' => 'custom/modules/Contacts/js/customValidation.js',
),
),
...

Steps to add custom form validation with UnderscoreJS:

  • Step 1: Include underscoreJS and custom validation in your metadata (current example: custom/modules/Contacts/metadata/editviewdefs.php)
  • Step 2: Use _.wrap function to add your custom validation before of after Sugar original check_form function which validates the form with Sugar rules (current example: custom/modules/Contacts/js/customValidation.js)
  • Step 3: Make a Quick Repair & Rebuild :-)

Additional links:

@yannick-b
Copy link

very useful thx!

@divyesh1221
Copy link

Hi cmourizard How to set field regular expression using customValidation.js

@divyesh1221
Copy link

how to set field validation

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