SugarCRM: Adding Javascript validation on form submit

Assume your module name as ‘ABC_XXX’.

Create the file or edit the existing file
/custom/modules/ABC_XXX/metadata/editviewdefs.php
and include the code below

$module_name = 'ABC_XXX';
$viewdefs [$module_name] =
array (
  'EditView' =>
  array (
    'templateMeta' =>
    array (
      'form' =>
      array(
         'buttons' =>
        array (
         0 =>
          array(
           'customCode' =>
            '<input title="Save [Alt+S]" accessKey="S" onclick="this.form.action.value=\'Save\'; return check_custom_data();" type="submit" name="button" value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
          ),
         1 =>
          array(
           'customCode' =>
            '<input title="Cancel [Alt+X]" accessKey="X" onclick="this.form.action.value=\'index\'; this.form.module.value=\''.$module_name.'\'; this.form.record.value=\'\';" type="submit" name="button" value="'.$GLOBALS['app_strings']['LBL_CANCEL_BUTTON_LABEL'].'">'
          )
        )
      ),
      'includes'=> array(
  array('file'=>'custom/modules/ABC_XXX/customJavascript.js'),
      ),
    ),
);

Create a javascript file at
custom/modules/ABC_XXX/ named customJavascript.js
and include the code below

function check_custom_data()
{
    alert('here');
    if (condition) {
        return false;
    }
    return check_form('EditView');
}
SugarCRM: Adding Javascript validation on form submit

27 thoughts on “SugarCRM: Adding Javascript validation on form submit

  1. Exceptional page and even quick to help you comprehend justification. Exactly how can As i continue acquiring concur to help you place part belonging to the guide inside new newssheet? Getting right credit standing you that journalist and even backlink with the internet site won’t even be a concern.

    Like

  2. I don’t even know how I ended up here, but I thought this post was good. I don’t know who you are but certainly you’re going to a famous blogger if you aren’t already 😉 Cheers!

    Like

  3. Great blog . . . I’m kind of a personal development junkie and was looking around on google blogsearch for stuff related to that. Definitely grabbed the rss feed and looking forward to reading more!

    Like

    1. I want to add javascript on save button and tried as explained in this blog.

      quickcreatedefs.php

      ‘customCode’ =>
      ”,
      …….
      ‘file’ => ‘modules/test_test/change.php’,

      but doesn’t work.

      The only way I managed to get him to go was to change the file /include/Smarty/plugins/function.sugar_button.php like that:

      case “SUBPANELSAVE”:
      $view = $view == ‘QuickCreate’ ? “form_SubpanelQuickCreate_{$module}” : $view;
      return ‘{if $bean->aclAccess(“save”)}{/if} ‘;

      That would be a solution but I think this not is customizing upgrade safe and for each module must change the structure.

      Like

    2. I want to add javascript on save button and tried as explained in this blog.

      quickcreatedefs.php

      ‘customCode’ =>
      ‘ <input title=”Save [Alt+S]” accessKey=”S” onclick=”this.form.action.value=\’Save\’;return check_custom_data();” type=”submit” name=”button” value=”‘.$GLOBALS[‘app_strings’][‘LBL_SAVE_BUTTON_LABEL’].'”>’,

      …….
      ‘file’ => ‘modules/test_test/change.php’,

      but doesn’t work.

      The only way I managed to get him to go was to change the file /include/Smarty/plugins/function.sugar_button.php like that:

      case “SUBPANELSAVE”:
      $view = $view == ‘QuickCreate’ ? “form_SubpanelQuickCreate_{$module}” : $view;
      return ‘{if $bean->aclAccess(“save”)}<input title=”{$APP.LBL_SAVE_BUTTON_TITLE}” accessKey=”{$APP.LBL_SAVE_BUTTON_KEY}” class=”button” onclick=”this.form.action.value=\’Save\’;if(check_custom_data(check_form(\”.$view.’\’)))return SUGAR.subpanelUtils.inlineSave(this.form.id, \” . $params[‘module’] . ‘_subpanel_save_button\’);return false;” type=”submit” name=”‘ . $params[‘module’] . ‘_subpanel_save_button” id=”‘ . $params[‘module’] . ‘_subpanel_save_button” value=”{$APP.LBL_SAVE_BUTTON_LABEL}”>{/if} ‘;

      That would be a solution but I think this not is customizing upgrade safe and for each module must change the structure.

      Like

    3. The file should be a JS file. include that file on the custom folder. the function you calling should be a JS function.
      Example
      includes’=> array(
      array(‘file’=>’custom/modules/ABC_XXX/customJavascript.js’),
      ),

      Like

    4. Thanks, but still is not working.

      change.js

      function myjs(){ alert(‘ok’) }.

      When I’m in editview and press the save button works fine, but when I’m in quickcreate don’t appear alert popup

      Like

    5. Thanks, but still is not working

      ‘file’ => ‘modules/test_test/change.js’,

      change.js

      function myjs()
      {
      alert(‘ok’);
      }

      Editview works fine when I press the save button, but in quickcreate does not appear popup alert.

      Thanks

      Like

Leave a comment