Set condition before clicking "Save" button in a module.

How do i set a condition on a button.
Lets say i have a field call "Numbers".
If the number exceeds 50, the user will not be allowed to save the record.
If the user clicks the "Save" an error message will appear on the screen saying" Exceed 50".

I am using sugar ce ver 6.5
  • Hi Yuenchee Chan,


    First you need create logic_hooks.php in custom/modules/

    In logic_hooks.php file:
    <?php
        $hook_version = 1;
        $hook_array = Array();

        $hook_array['before_save'] = Array();
        $hook_array['before_save'][] = Array(
            //Processing index. For sorting the array.
            1, 
            
            //Label. A string value to identify the hook.
            'before_entry_point example', 
            
            //The PHP file where your class is located.
            'custom/modules/logic_hooks_class.php',
            
            //The class the method is in. 
            'logic_hooks_class',
            
            //The method to call. 
            'before_entry_point_method' 
        );

    ?>


    Then you need to create logic_hooks_class.php in custom/modules/

    In logic_hooks_class.php File:

    <?php
        if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
       
        class logic_hooks_class
        {
            function before_entry_point_method($bean,$event, $arguments)
            {
                // here you need to code your requirement
            }
        }

    ?>
  • Hi thanks a lot for the help! May i know what are the codes required for me to display the error message?
  • I think logic hook is not the appropriate way for the validation and restrict user for saving record.

    You should have to manage this thing using the javascript. below are the steps by which you can do this validation.
    1. If editviewdefs.php file does not exist at custom/<module_name>/metadata/ directory then copy same file from the <module_name>/metadata/ directory to the custom/<module_name>/metadata/ .
    2. Find the 'SAVE' in button and replace it with your custom button.for example, 
               0 =>   array (
                    'customCode' => '<input type="submit" value="{$APP.LBL_SAVE_BUTTON_LABEL}" name="button" value="Save" id="SAVE_HEADER" 
    onclick="var _form = document.getElementById(\'EditView\'); _form.return_id.value=\'\'; _form.action.value=\'Save\'; if(custom_check_form(\'EditView\'))SUGAR.ajaxUI.submitForm(_form);return false;" class="button" accesskey="{$APP.LBL_SAVE_BUTTON_KEY}" title="{$APP.LBL_SAVE_BUTTON_TITLE}"/>',
                ),
         3.  Create a javascript file file at custom/js/custom-validation.js and create function           custom_check_form() to that file, for Example : 

              function custom_check_form(view)  {   
                    var validate = true;
                    if(  <Your condition>) {
                           alert("Your massage");
                           validate = false;
                     }
                    if(validate && check_form(view)) {
                           return true;
                    }
                    else {
                           return false;
                    }
             }
        4 .  Add this javascript file to the Editview using editviewdefs.php file. you may have to add below line to templateMeta of the editviewdefs.php file.  
    'includes' =>   array (
            0 => 
            array (
              'file' => 'custom/js/custom-validation.js',
            ),
    ),


    after doing this do't forget to do repair and rebuilt from Administrator.
  • My editviewdefs.php does not have the "Save" button code.
  • But it throws error of access when we create new record through sub panels (either quick create or full form)

    Let say I have written above code on my Opportunity Save button. It working fine for Opportunity Module. But when I create new record in Opportunity from Account Subpanel, After saving record in Opportunity module. It throws error like this -

    Error retrieving record. This record may be deleted or you may not be authorized to view it.
  • Hi  Aniruddh,

    I have followed your code in SpiceCRM it worked able to validate fields. But in Opportunity module default Mandatory fields are not showing error message when click save it continue to submit form data can u please help me to solve this.

  • Hi  Aniruddh,

    I have followed your code in SpiceCRM it worked able to validate fields. But in Opportunity module default Mandatory fields are not showing error message when click save it continue to submit form data can u please help me to solve this.