4

I just started using SugarCRM CE for the first time (Version 6.5.15 (Build 1083)). I'm quite impressed with the ease of use when adding new fields or modules, but there's one quite indispensable thing that seems to be missing: Validation of user input.

I would for example like to check a lot of things:

  • Check if a emailadres has a valid format, using some regular expression
  • Check if a postalcode exists (maybe do a webswervice call to validate it)
  • Do a calculation to see if a citizen service number is valid
  • etc.

The only thing I seem to be able to do in studio is make a field required or not, there doesn't seem to be any standard way to execute a validation on a field.

All I can find when I google on it is lots of ways to hack into the source code, like this one: http://phpbugs.wordpress.com/2010/01/22/sugarcrm-adding-javascript-validation-on-form-submit/ And even then I don't find any examples that actually do a validation.

Am I just missing something? Or is editing source code the only way to add this?

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
ErikL
  • 2,031
  • 6
  • 34
  • 57

4 Answers4

3

I don't think the "standard" validations are available in the CE edition.

What surprises me is that you can't define a validation somewhere and attach it to a field. I kind of expected this, since the rest of the system is very well structured (modules, packages, etc..)

I now for instance created a 11-check, this is a very specific check for a dutch bank account number. to get this to work, I did the following (based upon examples I found googling around):

I added the bank account to contacts in studio and after that edited \custom\modules\Contacts\metadata\editviewdefs.php

I added the following snippets:

'includes'=> array(
        array('file'=>'custom/modules/Contacts/customJavascript.js')),




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'].'">'
          )
        ),

And in customJavascript.js i placed this code:

function check_custom_data()
{
    if (!eleven_check(document.getElementById("bankaccount_c").value)){
        alert ('Bank account not valid');
        return false;
    } else {
      return check_form('EditView');
    }

    function eleven_check(bankaccount) {
    bankaccount=bankaccount.replace(/\D/, "");
    charcount=bankaccount.length;
    var som=0;
    for (i=1; i<10; i++) {
        getal=bankaccount.charAt(i-1);
        som+=getal*(10-i);
    } 
    if (som % 11==0 && charcount==9) {
        return true
    } else {
        return false
    }
}

}

This check now works the way I want it to work, but I'm wondering if this is the best way to add a validation. this way of adding a validation doesn't however accommodate PHP validations, for instance, if I want to validate against some data in the database for one or another reason, I would have to use ajax calls to get that done.

ErikL
  • 2,031
  • 6
  • 34
  • 57
  • Instead of "alert", you may use the Sugar standard notification using `add_error_style()` function. First of all, call `clear_all_errors();` to clear all the form errors, then call the function to view the error on the field: `add_error_style('EditView', 'bankaccount_c','Bank account not valid' );` – Lipsyor Apr 04 '17 at 09:18
0

Email validation is in the pro edition, I had assumed it was in CE as well but I'm not 100% sure. The other 2 are a lot more specific - postcode validation would depend upon your country so would be difficult to roll out. For these you will need to write your own custom validation.

MartinTawse
  • 321
  • 2
  • 10
0

I know its late, but maybe still someone needs this.

You can just add your custom javascript validation as a callback in your vardefs like this:

'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;}',
),

I documented it here as its not well documented elsewhere:

https://gunnicom.wordpress.com/2015/09/21/suitecrm-sugarcrm-6-5-add-custom-javascript-field-validation/

Gunni
  • 519
  • 5
  • 14
-2

You can add custom validation code to the following file: ./custom/modules/.../clients/base/views/record/record.js

There you can add validation code. In this example, I will validate if the phone_number is not empty when an accounts has a customer-type:

EXAMPLE CODE IN RECORD.JS:

({
    extendsFrom: 'RecordView',
    initialize: function (options) {
        app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});

        //add validation
        this.model.addValidationTask('check_account_type', _.bind(this._doValidateCheckType, this));
    },
    _doValidateCheckType: function(fields, errors, callback) {
        //validate requirements
        if (this.model.get('account_type') == 'Customer' && _.isEmpty(this.model.get('phone_office')))
        {
            errors['phone_office'] = errors['phone_office'] || {};
            errors['phone_office'].required = true;
        }
        callback(null, fields, errors);
    }
})

Don't forget to repair en rebuild!

The full documentation can be found here

Laurent
  • 523
  • 6
  • 15