AnsweredAssumed Answered

How to add a validation task for a field in a button click event?

Question asked by Octavio García Robles on Sep 10, 2017

Hi!

In order to complete a user requirement I need to modify the create-actions controller in which for the basic flow I already add validation tasks for many of the fields. For the requirement I need to validate only one of those fields but only in a button click event (in a button that I already added) in wich if the field is not empty I will call another function that I use to generate a PDF but in case of the field is empty I need to set the error style to that field and show the tooltip and the error alert just like the addValidationTask method

 

I have a code like this:

 

({

    extendsFrom: 'CreateView',
    initialize: function (options) {
        this._super('initialize', [options]);
        ...
        this.model.addValidationTask('validateField', _.bind(this._validateField, this));
        ... //more validation tasks
        //add listener for custom button
        this.context.on('button:myButton:click', this.myButtonFunction, this);
    },
    ...
    _validateField: function (fields, errors, callback) {
        if (_.isEmpty(this.model.get('my_field'))) {
            errors['my_field'] = errors['my_field'] || {};
            errors['my_field'].required = true;
        }
        callback(null, fields, errors);
    },
    ...
    myButtonFunction: function(e) {
        var self = this;

        if (_.isEmpty(self.model.get('my_field'))) {
            /*
            * TODO
            */
       } else {

          self.generatePDF(self.model.get('id'));

       }
    },
})

Outcomes