Search code examples
smartclient

Validate Form on Change ONLY


Because much of our imported data technically has validation errors, users are unable to update fields without first correcting previously entered bad data. This wouldn't be a problem except that many times this user doesn't have the information needed to enter a correct value into that field but we still need to save their update.

Is it possible to disable the validate on submit for a DynamicForm?


Solution

  • The best solution I could find thus far.

    I'm disabling validation and overridding getValues, which is called as part of saveData so I manually parse through any fields and look for errors. If I find an error I remove it from the return value and store it under the valuesManager.invalidatedFields.

    If a field had an error it will not be included in the save, but because the server will return the original value I had to override setValues as well to prevent your (bad) change from being overridden.

    Also, because getValues is called on initial load it validates on load as well.

    isc.ValuesManager.create({
        disableValidation: true,
        invalidatedFields: {},
        setValues: function(values){
            console.log("setting values..", this.invalidatedFields);
            for (var key in this.invalidatedFields) {
                if (this.invalidatedFields.hasOwnProperty(key)) {
                    values[key] = this.invalidatedFields[key];
                }
            }
            this.Super("setValues", arguments);
        },
        getValues: function () {
            this.invalidatedFields = [];
            var data = this.Super("getValues");
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    var form = this.getMemberForField(key);
                    if (form && !form.getField(key).validate()) {
                        console.log(key + " failed validation", data[key]);
                        this.invalidatedFields[key] = data[key];
                        delete data[key];
                    }
                }
            }
            return data;
        }
    });