AnsweredAssumed Answered

Sugar 7 Enterprise - Preventing form from submiting

Question asked by Ramon Marcondes on Jun 13, 2016
Latest reply on Oct 18, 2016 by Ramon Marcondes

Hello.

I'm using SugarCRM Enterprise 7.5.

I already found a way to validate related fields from the model RevenueLineItems using javascript (backbone.js + underscore.js), and show an error message on the screen. It's being used on the EditView.

But I have some problems:

  • If I try to force an error to a field and call the 'callback' function, the form submits, when it shouldn't. The error isn't for the Revenue model, but to a related Account model field (the relationship is RevenueLineItems -> Opportunities -> Accounts).
  • If I put a 'return false;' to the validation, the Save button becomes blocked, that's why I commented the 'return' code.

 

How do I prevent the form from submitting in a specified condition, without blocking the Save button?

 

Here is the code (custom/modules/RevenueLineItems/clients/base/views/record/record.js):

({     
    extendsFrom: 'RevenueLineItemsRecordView',    
    
    initialize: function (options) {      
        this._super('initialize', [options]);   
        //add custom message key     
        //app.error.errorName2Keys['custom_message'] = 'ERR_CUSTOM_MESSAGE';  
        //add validation    
        //console.log("initialize");       
        this.model.addValidationTask('check_account_type', _.bind(this._doValidateCheckType, this));           
    },     
    _doValidateCheckType: function(fields, errors, callback) { 
        var receita = app.data.createBean('RevenueLineItems', {id: this.model.id});
        var request = receita.fetch();
        
        request.xhr.done(function () {            
            //console.log('Receita: '+receita.get('id') );
            var receita_op = receita.getRelatedCollection('opportunities');
            var opportunities = receita_op.fetch();
            opportunities.xhr.done(function () { 
                //console.log(receita_op);                
                for (var i = 0; i < receita_op.length; ++i) {
                    var opportunity = receita_op.models[i];
                    var op_accounts = opportunity.getRelatedCollection('accounts');
                    var accounts = op_accounts.fetch();
                    accounts.xhr.done(function () {  
                        //console.log(op_accounts);
                        
                        for (var i = 0; i < op_accounts.length; ++i) {
                            var account = op_accounts.models[i];
                            if (receita.get('sales_stage') == 'Closed Won'
                                && (account.get('id') != ""
                                || account.get('cnpj_c') != "")) {
                                    errors['sales_stage'] = errors['sales_stage'] || {};
                                    errors['sales_stage'].field_error = true;
                            
                                    app.alert.show('message-id', {
                                        level: 'error',
                                        messages: 'CNPJ required',
                                        autoClose: false
                                    });                                      
                                    // return false;
                            }
                        }
                    });                                        
                }
            });             
        });
        callback(null, fields, errors);        
    }  
})  

 

Any help would be appreciated.

Thanks.

Outcomes