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');     }, })
  • Thank you, Jerry! That did the trick!
    I'm currently working with 7.1.5 and only had to add a module check before calling the validation function (this.model.module == 'Accounts').
    Hope there is a better module placed solution soon!
Reply Children
No Data