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