Sugar 7 - Form submitting before client-side validation

Hello.

I'm using SugarCRM Enterprise 7.5 for production system, and 7.7 for the test system.

I'm developing a package for form validation in the RevenueLineItems (it's called 'Receita' in brazilian portuguese) native module.

I followed exactly what this tutorial (link below) shows for validating form fields (to see the video, it needs a quick registration):

Registration

The difference is that in the tutorial, the form only submits after successful validation, but for me it is submitting even before the validation!

The manifest is set correctly, as the record.js file is called on the record EditView page.

I've uploaded a zip file (ReceitaCodeApi.zip) with all the used files if someone is interested on checking the code, especially the record.js, which does the validation.

Another thing, the custom error message is not working. I've done the same as this tutorial:

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/UI_Model/Views/Examples/Adding_Field…

It's only showing the default error message.

What do I need to do to validate first, and save only if validation is correct?

I've already tried "return false" and "preventDefault", but they block the form from saving in the next try...

Any help would be appreciated.

Thanks.

_ReceitaCodeApi.zip
Parents
  • Sorry I'm late to this, try this:

    ({
        extendsFrom: 'RevenueLineItemsRecordView',
        initialize: function (options) {
            this._super('initialize', [options]);
            // add custom message key    
            app.error.errorName2Keys['custom_message'] = 'ERR_CUSTOM_MESSAGE';

            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 receita_model = this.model;
            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];
                                console.log(/*"Proposta: "+receita.get('sales_stage')+*/" - Account: " + account.get('id') + " - " + account.get('cnpj_c'));

                                // PROBLEMA: ESTÁ SALVANDO MESMO FORÇANDO OS ERROS!

                                if (receita.get('sales_stage') == 'Closed Won'
                                        /*&& (!account.get('id') || !account.get('cnpj_c'))*/) {
                                    console.log("Erro");
                                    receita_model.trigger('error:cnpj_required:sales_stage');

                                    errors['sales_stage'] = {};
                                    errors['sales_stage'].custom_message = true;

                                    callback(null, fields, errors);

                                    app.alert.show('cnpj_required', {
                                        level: 'error',
                                        messages: 'A Oportunidade da Receita necessita de um Cliente com CNPJ',
                                        autoClose: false
                                    });

                                } else {
                                    console.log("Ok");
                                }
                            }
                            callback(null, fields, errors);

                        });
                    }
                });
            });
        }
    }) 
Reply
  • Sorry I'm late to this, try this:

    ({
        extendsFrom: 'RevenueLineItemsRecordView',
        initialize: function (options) {
            this._super('initialize', [options]);
            // add custom message key    
            app.error.errorName2Keys['custom_message'] = 'ERR_CUSTOM_MESSAGE';

            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 receita_model = this.model;
            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];
                                console.log(/*"Proposta: "+receita.get('sales_stage')+*/" - Account: " + account.get('id') + " - " + account.get('cnpj_c'));

                                // PROBLEMA: ESTÁ SALVANDO MESMO FORÇANDO OS ERROS!

                                if (receita.get('sales_stage') == 'Closed Won'
                                        /*&& (!account.get('id') || !account.get('cnpj_c'))*/) {
                                    console.log("Erro");
                                    receita_model.trigger('error:cnpj_required:sales_stage');

                                    errors['sales_stage'] = {};
                                    errors['sales_stage'].custom_message = true;

                                    callback(null, fields, errors);

                                    app.alert.show('cnpj_required', {
                                        level: 'error',
                                        messages: 'A Oportunidade da Receita necessita de um Cliente com CNPJ',
                                        autoClose: false
                                    });

                                } else {
                                    console.log("Ok");
                                }
                            }
                            callback(null, fields, errors);

                        });
                    }
                });
            });
        }
    }) 
Children
  • Hello, Alan.

    I found another way to solve this:

    In the PHP logic hook (before_save), when reaching the condition I want, I put the following lines of code:

    require_once 'include/api/SugarApiException.php';
    throw new SugarApiExceptionInvalidParameter("Validation error message");

    When trying to submit the form, it shows the message I put in the exception, preventing the form from submitting.

    I don't know if it's the best way to do it, but it was the only way that really worked for me.

    Anyway, thanks for the help.

    Best Regards,

    Ramon Marcondes