SuiteCRM / SugarCRM 6.5 add custom javascript field validation

If you need some custom validation for a field in SuiteCRM or SugarCRM 6.5, and dont find a solution. Its not your fault, its just not well documented. After some digging in code i found a very good solution.

If you want to do a custom javascript check before the EditView is saved, just add something like the following to your vardefs.php:

    'validation' => 
    array (
      'type' => 'callback',
      'callback' => 'function(formname,nameIndex){if($("#" + nameIndex).val()!=999){add_error_style(formname,nameIndex,"Only 999 is allowed!"); return false;}; return true;}',
    ),

Then do just a “Quick Repair and Rebuild” and you are done.
I hope its easy to understand, so you can adapt it to your needs.
Easy? Yes! Easy to find out? No.
But now you know.

Advertisements

, , ,

  1. #1 by Ray on November 2, 2015 - 17:03

    Hi,
    interesting however for newbie like me it’s not clear to put the codes.

    Could you please add some additional information?

    Thank you

    • #2 by gunni on November 2, 2015 - 17:06

      What information do you need?
      Each module has a vardefs.php where the fields are defined. Within a field definition just add code like the above.

  2. #3 by Ray on November 2, 2015 - 17:22

    I need to know if its upgrade save….so may i need to copy vardefs.php in my custom folder?
    Any changes is needed?like filed name or whathever?
    if($(“#” + nameIndex) # must be replaced with anything else?

    Thank you

    • #4 by gunni on November 2, 2015 - 17:33

      If you need to make it upgrade safe, the change must be done in the custom folder (Or within the custom/modulebuilder if its a complete custom module from you)
      “nameIndex” can stay like this and is handled automatically.

  3. #5 by Ray on November 2, 2015 - 17:35

    so just for example….i have vardefs.ext.php in my custom Ext folder and i put something like that:

    $dictionary[‘Lead’][‘fields’][‘dbgit_saccountname_c’][‘inline_edit’]=’1′;
    $dictionary[‘Lead’][‘fields’][‘dbgit_saccountname_c’][‘labelValue’]=’Shipper Account Name’;
    $dictionary[‘Lead’][‘fields’][‘dbgit_saccountname_c’] => array (
    ‘validation’ =>
    array (
    ‘type’ => ‘callback’,
    ‘callback’ => ‘function(formname,nameIndex){if($(“#” + nameIndex).val()!=999){add_error_style(formname,nameIndex,”Only 999 is allowed!”); return false;}; return true;}’,
    );
    )

    is it correct cause it not working..

    Thank you

    • #6 by gunni on November 2, 2015 - 17:53

      In your comment below you say its working.
      In this code i see one error. You got one “array()” too much.

      $dictionary[‘Lead’][‘fields’][‘dbgit_saccountname_c’] [‘validation’] =>
      array (
      ‘type’ => ‘callback’,
      ‘callback’ => ‘function(formname,nameIndex){if($(“#” + nameIndex).val()!=999){add_error_style(formname,nameIndex,”Only 999 is allowed!”); return false;}; return true;}’,
      );

      But if you got it working now, its just fine.

  4. #7 by Ray on November 2, 2015 - 17:50

    Ok find it….the right place to put the code in my case is…

    custom..extension…modules…mudle name….vardefs.php

    Thank you for pointing me on the right way

  5. #8 by Ray on November 2, 2015 - 17:59

    you’re right…thank you

    and what about checking if at least one field is not null….

    I’ve 2 fields…and i need that at least one in filled by the employee….not necessary both…

    Do you think is applicable?

    • #9 by gunni on November 2, 2015 - 19:25

      You may check for empty value, maybe not for null values.

  6. #10 by Ray on November 2, 2015 - 23:11

    mmm… i think something is nor clear..

    i’ve tried in many ways:

    if($(“#” + nameIndex).val()==””)

    if($(“#” + nameIndex).val().length==0)

    if($.trim(!$(“#dbgit_saccountname_c”)))

    if($.trim(!$(“#dbgit_saccountname_c”).val()))

    if($.trim($(“#dbgit_saccountname_c”).val().length===0))

    if($.trim($(“#dbgit_saccountname_c”).val())==””)

    but nothing seems to trigger an empty string

    And …after that i would like to achieve this:

    field … validation…if at least one is non empty ok…otherwise validation warning on both input fields.

    Thank you if you would to reply me with any comments

    • #11 by gunni on November 2, 2015 - 23:16

      Oh sorry. Forgot to mention that sugar first checks for empty fields and so wont do the custom validation.
      I fixed this for me, and hopefully it will get into SuiteCRM:
      https://github.com/salesagility/SuiteCRM/pull/491
      But this is a not upgrade safe code change.

  7. #12 by Ray on November 2, 2015 - 23:13

    sorry….typo during my reply….i wanted to have 2 fields…and if both are empty that would warn me on both fields that at least one must be filled….

  8. #13 by Ray on November 3, 2015 - 10:01

    AH ok….so you mean that there’s no chance to make it upgrade safe….so i think the only way is to do a custom validation on vefore:submit() jquery with an external script…..Are u agree with me?

  9. #15 by Ray on November 3, 2015 - 10:13

    anyway…just to test i’ve tried your patch code…sugar_3.js…but it still not working.

    I’m trying to validate a test email address and check if its empty….just for test…and once done then i will do a more complex validation.

    • #16 by gunni on November 3, 2015 - 12:54

      Did you replace both files, the normal and the minified?
      Did you do a Quick-Repair and Rebuild?

  10. #17 by Ray on November 3, 2015 - 17:44

    no…i forgot to change also minified…

    Anyway trying an upgrade safe solution i’ve also tried the link you posted with no luck.Maybe because i’ve already a customization in view.edit.php and maybe something interact bad with this.

    class LeadsViewEdit extends ViewEdit
    {

    function display(){
    require_once ‘modules/ACLRoles/ACLRole.php’;

    bla
    bla
    bla
    //Call the parent display function
    parent::display();
    }

    }

    Adding function predisplay i’m not able to realize a custom validation.

    class LeadsViewEdit extends ViewEdit
    {
    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
    }

    function display(){
    require_once ‘modules/ACLRoles/ACLRole.php’;

    bla
    bla
    bla
    //Call the parent display function
    parent::display();
    }

    }

    Maybe i’m not coding right

  11. #18 by Michael Joyner on November 16, 2016 - 01:46

    Nice post. Man, I wish I knew about this 2 years ago.

    Gunni………… do you have Zucker Reports V2 package around still ?

    I saw your repo on GitHub

    michaelwjoyner_at_gmail.com

    • #19 by gunni on November 16, 2016 - 20:02

      Sorry, that i do not have the V2 package anymore. I abandoned ZuckerReports some years ago.

  1. SuiteCRM / SugarCRM 6.5 add custom javascript field validation | SutoCom Solutions

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: