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);
    }
})
  • 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?
  • Hi paperless,

    The customCode attribute is not applicable to Sidercar modules. It would only work for modules in BWC mode.
  • 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