formio / formio.js

JavaScript powered Forms with JSON Form Builder
https://formio.github.io/formio.js
MIT License
1.88k stars 1.06k forks source link

Custom Component not submitting key/value on empty submission #2509

Closed gopi-sedin closed 8 months ago

gopi-sedin commented 4 years ago

Hello, I have a custom component which is supposed to store hidden data. The user will not be entering any data to this field but will be modified by our javascript logic. How ever if there is no data entered in this field then the submitted JSON data doesn't have these key/value pairs.

in the below example, if I manually enter any data in the History textfield, only then the key/value pair is sent. Any idea on how to always get the data irrespective of the data entered? I am facing the same issue in all of my custom components


var InputHistroyComponent = Formio.Components.components.input;

function HistroyComponent(component, options, data) {
    InputHistroyComponent.prototype.constructor.call(this, component, options, data);
}

HistroyComponent.prototype = Object.create(InputHistroyComponent.prototype);
HistroyComponent.prototype.constructor = HistroyComponent;

HistroyComponent.schema = function () {
    return InputHistroyComponent.schema({
        title: 'History',
        type: 'histroycomponent',
        hideLabel: true,
        label: 'Histroy'
    });
};

HistroyComponent.builderInfo = {
    title: 'History',
    group: 'basic',
    icon: 'fa fa-table',
    weight: 70,
    documentation: '#',
    schema: HistroyComponent.schema()
};

HistroyComponent.prototype.render = function (element) {
    var tpl = '<div style="display:block">';
    tpl += this.renderTemplate('input', {
        input: {
            type: 'input',
            attr: {
                type: 'text',
                class: 'txtHistory'
            },
            id: 'txtHistory',
            default:'~'
        }
    });
    tpl += this.renderTemplate('input', {
        input: {
            type: 'hidden',
            attr: {
                type: 'hidden',
                class: 'hdnStatus'
            },
            id: 'hdnStatus',
            default: ''
        }
    });
    var tpl = '</div>';
    return tpl;
};

HistroyComponent.editForm = function () {
    return {
        components: [
        ]
    };
}

HistroyComponent.prototype.setValue = function (value) {
    this.refs.input[0].value = value.txtHistory;
    this.refs.input[1].value = value.hdnStatus;
}

HistroyComponent.prototype.getValue = function () {
    var value = {};
    value = {
        txtHistory: this.refs.input[0].value,
        hdnStatus: this.refs.input[1].value,
    };
    return value;
};
Formio.Components.addComponent('histroycomponent', HistroyComponent);
wag110894 commented 4 years ago

Hello @gopi-sedin,

Thank you for reaching out!

We do offer Support Packages that are for training, support or development services. The hours can be used in one hour increments at your convenience over a 90 day period. We will assign one of our senior developers to work directly with you in online sessions. Please contact support@form.io if you are interested.

Thank you.

Tatsiana8 commented 8 months ago

Closing this thread as it is outdated. Please re-open if it is still relevant. Thank you for your contribution!