SugarCRM: Create a custom javascript validation before saving in Edit view

This note will teach you how to call a custom javascript function upon clicking the Save button in Edit view.

1. Create a custom view.edit.php in custom/modules/<module>/view/ and create the javascript function under the preDisplay function. See sample code bellow:

public function preDisplay() {
            parent::preDisplay();

// Javascript function to call

function customJavascriptValidation() {
// Insert code here

return check_form(“Edit View”); // This will call the default function that sugar use
}

2. Go tot editviewdefs.php in custom/modules/metadata/ and put the codes below before the widths array:

'form' => 
      array (
        'buttons' => 
        array (
          0 => array (
            'customCode' => '<input type="submit" name="save" id="save" onClick="this.form.return_action.value=\'DetailView\'; this.form.action.value=\'Save\';  return custom_function(\'EditView\');" value="Save">',
          ),
        ),
      ),

* if file doesnt exist then copy the ediviewdefs.php from modules/<module>/metadata/

3. Run Quick Repair and Rebuild.

Tags: ,

6 responses to “SugarCRM: Create a custom javascript validation before saving in Edit view”

  1. António Santos says :

    On Sugar 5.5.4 I had:
    $(‘#EditView’).submit(customValidations());

    And now, with Sugar 6.5.12, I need to do this?!
    I think this is not so simple than before…😦

  2. johndopeit says :

    I dont know about lower versions of 6 but in 6.5, yes you should.

  3. aviram says :

    Doesn’t work at all , what do i do with the CANCEL button that doesn’t show now. where is it in the sample code above? how do you insert a javascript function code inside a php function and make it work? (customJavascriptValidation)

  4. Bill Convis says :

    Customer of ours asked us to do this and after a few hours, we got it to work – here are the instructions. This is for SugarCRM CE 6.5.22. Here is the scenario:

    A custom module called Atask_Advertiser_Task has four fields called survey_question_1_c
    survey_question_2_c
    survey_question_3_c
    survey_question_4_c
    It the status and type of the Advertiser Task are set to a specific value, we want to make sure that the four survey questions have a value.

    First step:
    In the /custom/modules/Atask_Advertiser_Task/metadata/editviewdefs place the following code after maxColumnsand before widths:

    ‘maxColumns’ => ‘2’,
    ‘form’ =>
    array (
    ‘buttons’ =>
    array (
    0 => array (
    ‘customCode’ => ”,
    ),
    ‘CANCEL’,
    ),
    ),
    ‘widths’ =>

    You will notice that the function customJavascriptValidation will be called

    Second Step:
    If the file view.edit.php does not already exist for your module, create it.
    For our project we created the following file:
    /custom/modules/Atask_Advertiser_Task/views/view.edit.php

    The contents of the file are:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    require_once('include/MVC/View/views/view.edit.php');

    class CustomAtask_Advertiser_TaskViewEdit extends ViewEdit {
    public function preDisplay() {

    parent::preDisplay();
    $out = <<<EOQ

    function customJavascriptValidation() {
    var type = $(“#type”).val();
    var status = $(“#status”).val();
    if((type == ‘Affiliate_Info_Page’ || type == ‘Offer_Creative_Development’) && (status == ‘Active_Completed’)){
    var survey_question_1_c = $(“#survey_question_1_c”).val();
    var survey_question_2_c = $(“#survey_question_2_c”).val();
    var survey_question_3_c = $(“#survey_question_3_c”).val();
    var survey_question_4_c = $(“#survey_question_4_c”).val();
    if(survey_question_1_c == ” || survey_question_2_c == ” || survey_question_3_c == ” || survey_question_4_c == ”){
    alert(‘This Advertiser Tasks requires that the Creative Evaluation be completed before this task can be closed.’);
    return false;
    }
    }
    return check_form(‘EditView’);
    }

    EOQ;
    echo $out;
    }

    }

    Notice that we create the javascript function customJavascriptValidation in the tag. If any of the custom text fields for survey questions are blank then post a message and return false, else return the normal process to complete the check form.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: