How can I validate fields in Accounts when creating new records?

I found some working example of how to add validation to the record.js that executes after the Save button is hit. However, it was later discovered, this validation was not working when first creating a new record (module is Accounts).
I tried including the slightly modified code to custom/modules/Accounts/clients/base/views/create/create.js but it seems like the action is not invoked?
What is the right action to extend when creating an Account record? Anything I'm doing wrong?

({    extendsFrom: 'CreateView',
    initialize: function (options) {
  console.log('Initializing Create View'); //trying to see if action is invoked
        app.view.invokeParent(this, {type: 'view', name: 'create', method: 'initialize', args:[options]});

        //add validation
        this.model.addValidationTask('check_fieldname', _.bind(this._doValidateCheckFieldName, this));
    },

    _doValidateCheckFieldName: function(fields, errors, callback) {
        //validate requirements
  var msg = '';
  var count_errors = 0;
  var field_val = this.model.get('field_c');
 
  /* Validation check goes here */
 
        if (count_errors != 0)
        {
            errors['field_c'] = msg;
            errors['field_c'].required = true;
        }
        callback(null, fields, errors);
    }
})
  • hI,Jerry
    I copy your code of create-actions.js into my instance ,after repair and rebuild ,it show a blank page when I create and it's ok to edit.It seems that I have a same problem with Larent and Jason.
    Can you or anyone give some advice ?It looks like many peaple have this problem.
    Or I missed some steps?
  • My recommendation is to make sure you are on 7.2.x and that you follow the updated article for 7.2 found here:

    http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_7.2/20_UI_Mode...
  • I just wanted to let everyone know that there are some updates to the validation article:
    http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_7.2/20_UI_Mode...

    If you are experiencing a blank page on the create view, there is an issue where having dependencies on the form can cause the page not to render. This has been corrected in 7.2.1. To correct this in 7.2.0, you can put the following in your initialize function above where you invoke the parent:

    if (!_.has(options.meta, "template"))
    {
        options.meta.template = 'record';
    }

    Changes made to the article consist of:
    • correction for bug noted above
    • app.view.invokeParent(this, {type: 'view', name: 'create', method: 'initialize', args: [options]}); is now this._super('initialize', [options]);
    • extendsFrom: 'CreateView', is now extendsFrom: 'CreateActionsView',
    I hope this helps!


  • Hello Jerry,

     I am trying  to make a field mandatory in Account Module  if login user is in Team say XYZ (which is created using Team module of SugarCRM), Now, in 6.7.5 I have achieved it using code below in view.edit.php

    $validation_js =  <<<JS  <script type="text/javascript">   
      var teamTEST="$teamTEST" ; 
    JS;
    Where $teamTEST" ; contain the team members

    Now, I really do not know in which file I should use above code in 7.2.2 (i.e to use php to get the team members and then check the team member with login user  in java script code to make field mandatory if login user exists in the team).

     I am able to  get the current user id  using "App.user.id"  in custom/modules/Cases/clients/base/views/records/record.js file,but I need to check that login user in team say XYZ.
    Also,am I working on correct file?

    So, please help in this issue, as it is urgently required.

    Thanks 
    Dikshit 
  • Hi Dikshit,

    The easiest way to accomplish this is to first create an endpoint for the team check. This endpoint would essentially accept a team id or name and return true or false if the current user is part of a given team. 

    Next, you will need to define your modules controller override and add in the validation method as shown here. For our purposes, I'll just say we created a method named _doValidateTeamCheck

    _doValidateTeamCheck: function(fields, errors, callback) {          var id = this.model.get("id");          //if no id, skip since we may be creating the record         if (_.isEmpty(id))         {             callback(null, fields, errors);             return;         }          var url = app.api.buildURL('CustomTeamCheck')          //show progress message         app.alert.show('team-check-progress', {             level: 'info',             messages: 'Checking Teams...',             autoClose: false         });    //add team ids to check against         params = {};          //check         var result = app.api.call("read", url, params, {             success: _.bind(function(data) {                  //close alert message                 app.alert.dismiss('team-check-progress');                  if (data.response == true)                 {                     app.alert.show('team-error-message', {                         level: 'error',                         messages: 'You are not a member',                         autoClose: false                     });                 }                 else                 {                  //continue with save                     callback(null, fields, errors);                 }             }, this),              error: _.bind(function(data) {                 //close alert message                 app.alert.dismiss('team-check-progress');                  //throw error alert                 app.alert.show('team-check-error', {                     level: 'error',                     messages: 'There was an error fetching.',                     autoClose: false                 });             }, this)         });

    The way this works is to not call callback(null, fields, errors); until you are ready to allow the user to save. Please note that i haven't specifically tested this code and there may be errors.
  • Thanks a lot  Jerry,
    I am checking this on my localhost and update you about my findings asap.
    Also,

     I had successfully  installed the sugar 7.2.1 on Localhost  and it was running fine, Now  recently I have installed 7.7.2 on local host properly, but when ever I will do repair and  rebuild, it will automatically remove some files from caches directory  and give error , as as shown in attached snapshot.
    So please tell me, is there any other configuration level  changes for 7.2.2, to see the changes made in code on local host?

    Some  files that are deleted or do not work after repair and rebuild are at -
    /var/www/html/sweaver168782-1408760315/cache/api/metadata/lang_en_us_base_public_ordered.json
    /var/www/html/sweaver168782-1408760315/cache/api/metadata/lang_en_us_base_ordered.json
    /var/www/html/sweaver168782-1408760315/cache/javascript/base/components_219828a0142659fc0505751584776fcd.js
    /var/www/html/sweaver168782-1408760315/cache/javascript/base/components_ed27bdcc828dfb5a3a39dc531c262832.js 

    Thanks 
    Dikshit
  • Hi Dikshit,

    Please create a new thread for your caching question as it does not apply to validating fields. My recommendations are to check your web permissions and that your environment meets our supported platforms found here: http://support.sugarcrm.com/05_Resources/03_Supported_Platforms/ (Especially the PHP version).
  • Hi all,

    Im new to Sugar. I tried to validate some fields in Account module and it is just working while I'm creating a new record, but it does not validate fields while I'm editing the current records. The code I've followed is as below. 

    Any help will be appreciated!
    Thank You!

    Create-action.js
    -------------------------------------------------------------------------------------------------------------

    ({    extendsFrom: 'CreateActionsView',

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

    this._super('initialize', [options]);

            //add validation task
            this.model.addValidationTask('check_phone_office', _.bind(this._doValidatePhone, this));
            this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this));
            
        },


        _doValidatePhone: function(fields, errors, callback) {

            if (_.isEmpty(this.model.get('phone_office')))
            {
                errors['phone_office'] = errors['phone_office'] || {};
                errors['phone_office'].required = true;
            }

            callback(null, fields, errors);
        },
        
         _doValidateEmail: function(fields, errors, callback) {

            if (_.isEmpty(this.model.get('email')))
            {
                errors['email'] = errors['email'] || {};
                errors['email'].required = true;
            }

            callback(null, fields, errors);
        },

    })

    ------------------------------------------------------------------------------------------------------------------
    Record.js


    ({    extendsFrom: 'RecordView',

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

    this._super('initialize', [options]);

    //add custom message key
    app.error.errorName2Keys['custom_message'] = 'ERR_CUSTOM_MESSAGE';

            //add validation
            this.model.addValidationTask('check_phone_office', _.bind(this._doValidatePhone, this));
            this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this));
            
        },
       

        _doValidatePhone: function(fields, errors, callback) {

            if (_.isEmpty(this.model.get('phone_office')))
            {
                errors['phone_office'] = errors['phone_office'] || {};
                errors['phone_office'].custom_message = true;
            }

            callback(null, fields, errors);
        },
        
         _doValidateEmail: function(fields, errors, callback) {

            if (_.isEmpty(this.model.get('email')))
            {
                errors['email'] = errors['email'] || {};
                errors['email'].custom_message= true;
            }

            callback(null, fields, errors);
        },

    })
1 2 3