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);
    }
})
Parents
  • This is in fact an issue that we are working to address in Sugar as well as the documentation. In 7.1.6, I believe you will need to also extend and manipulate the create-actions.js file as you would record.js

    custom/clients/base/views/create-actions/create-actions.js
    ({     extendsFrom: 'CreateView',          initialize: function (options) {         app.view.invokeParent(this, {type: 'view', name: 'create', method: 'initialize', args: [options]});               this.model.addValidationTask('class_validate', _.bind(this._doValidateClass, this));     },          _doValidateClass: function(fields, errors, callback) {      //logic     },          _dispose: function() {         this._super('_dispose');     }, })
  • When I add validation with 'create-actions.js', the create page is blank... any solutions?
  • I take it "add javascript for validation directly in the metadata/editviewdefs.php using the customCode attribute" as shown back in http://forums.sugarcrm.com/f6/validation-logic-hook-43077/ does not apply to 7.x?
  • This reply was created from a merged topic originally titled  Custom validating a new Accounts record.  Following on from this thread: https://community.sugarcrm.com/thread/18823

    I have successfully performed custom validation for modified records but haven't managed to do so for when creating new records. It just displays a blank screen.

    I have updated to 7.2.0 as according to Jerry Clark this issue has been fixed. Can someone please confirm whether this is correct?
  • Hi akc,

    On my local 7.2.0 Pro instance, I am able to do the following:

    ./custom/modules/Accounts/clients/base/views/record/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);     },      _dispose: function() {         this._super('_dispose');     },  })
    ./custom/modules/Accounts/clients/base/views/create-actions/create-actions.js
    ({     extendsFrom: 'CreateView',      initialize: function (options) {         app.view.invokeParent(this, {type: 'view', name: 'create', 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);     },      _dispose: function() {         this._super('_dispose');     }, })
     
    After a Quick Repair, when i change the account type to "Customer" I am seeing the expected results for the phone_office field when creating/editing an account record. 

    Can you post your create-actions.js example so that we can take a look?
  • Thanks for your reply. I still get a blank screen on create of account. The edit record works fine still.

    My create-actions.js file is as follows:

    ({     extendsFrom: 'CreateView',          initialize: function (options) {         app.view.invokeParent(this, {type: 'view', name: 'create', method: 'initialize', args: [options]});               this.model.addValidationTask('class_validate', _.bind(this._doValidateCheckType, this));     },          _doValidateCheckType: function(fields, errors, callback) {      if($('.existingAddress').length==0){          errors['email'] = errors['email'] || {};             errors['email'].required = true;         }         callback(null, fields, errors);     },          _dispose: function() {         this._super('_dispose');     }, })
  • Hi akc,

    I don't seem to be having the same issues when applying your example code. Have you modified the core create-actions view or created a custom ./custom/clients/base/views/create-actions/create-actions.js file?

    I would also recommend that you adjust your code to validate the email attribute instead of the class. I have provided an example below:

    ./custom/modules/Accounts/clients/base/views/create-actions/create-actions.js
    ({     extendsFrom: 'CreateView',      initialize: function (options) {         app.view.invokeParent(this, {type: 'view', name: 'create', method: 'initialize', args: [options]});          //add validation task         this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this));     },      _doValidateEmail: function(fields, errors, callback) {          if (_.isEmpty(this.model.get('email')))         {             errors['email'] = errors['email'] || {};             errors['email'].required = true;         }          callback(null, fields, errors);     },      _dispose: function() {         this._super('_dispose');     }, })
  • Thanks for the example. 

    I have not modified the core create-actions view or created a custom create-actions.js under custom/clients.

    Would it be possible that the upgrade to 7.2 from 7.1.6 went wrong? Can you find out which file/s actually fixed the issue from 7.1.6? Maybe I could manually copy core files over?
  • Hi akc,

    I did want to say that a better approach for your customization might just be to create a vardef for the email field to make it required since you don't seem to have any dependencies on the email field:

    ./custom/Extension/modules/Accounts/Ext/Vardefs/requireEmail.php
    <?php  $dictionary['Account']['fields']['email']['required'] = true;
    That should give you the behavior you are looking for without the views.

    Regarding your question:
    I would first try installing a stock 7.2.0 instance to see if you can get it working correctly. If so, you can follow this article to get your instance migrated to a clean install: http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_7.2/73_Migrati...
  • Many thanks. That fixes Sugar's issue with making an email address required. Any tips on how to do that without using the views?

    The only reason I wanted to do custom validation was due to the fact I wanted to make sure the email address entered was unique on the system.

    I will try the migration later on and see if that manages to fix my issues.

    Regards
    Ash
  • 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
Reply
  • 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
Children
No Data