Datatable validation

Hello,

Is it possible to do the following?

I have a parent form and a datatable (SPDataTable1), and when parent form status dropdown value is changed to "Closed", if SPDataTable1 datatable status dropdown DOES NOT say "Completed" (for any items in the table), then a data validation comes up that says "All Sub-tasks must be completed before the parent form can be Closed".

Can someone please explain how I can do this? Thank you.

Hello @ParAvion,

You can try to add a field validator like this:

fd.field('DropDown1').validators.push({
    name: 'MyCustomValidator',
    error: 'Error message',
    validate: function(value) {
        if (value == 'Completed') {
            var relatedItems = fd.control('SPDataTable1')._widget.dataItems();
            for (let i = 0; i < relatedItems.length; i++) {
                if(relatedItems[i].Status !== 'Completed'){
                    return false
                }
            } 
        }
        return true;
    }
});

Or the form validator with the same logic.

@mnikitina this is what I've done but it's not working. Do you see any concerns?

fd.field('Status').push({
name: 'ActionsNotClosed',
error: 'All Actions & Tasks must be completed to resolve this case.',
validate: function(value) {
    if (value == 'Closed - Resolved') {
        var relatedItems = fd.control('SPDataTable1')._widget.dataItems();
        for (let i = 0; i < relatedItems.length; i++) {
            if(relatedItems[i].Action_x0020_Status !== 'Completed'){
                return false
            }
        } 
    }
    return true;
}

});

Thanks for checking.

@ParAvion,

Have you added the code inside fd.spRendered event?

Are there any errors in the browser console? Please share a screenshot.

@mnikitina this is the complete line of code:

image

@ParAvion,

Are there any errors in the browser console? Please share a screenshot.

@mnikitina sent you a PM...thanks.

Hello @ParAvion,

Looks like there is a syntax error in the code. You are missing closing brackets of the spRendered event function:

fd.spRendered(function () {
    fd.field('Status').push({
    name: 'ActionsNotClosed',
    error: 'All Actions & Tasks must be completed to resolve this case.',
    validate: function(value) {
        if (value == 'Closed - Resolved') {
            var relatedItems = fd.control('SPDataTable1')._widget.dataItems();
            for (let i = 0; i < relatedItems.length; i++) {
                if(relatedItems[i].Action_x0020_Status !== 'Completed'){
                    return false
                }
            } 
        }
        return true;
    }
    });
});

@mnikitina thank you for the correction. Unfortunately it's still not working. Perhaps it's because I'm trying to do it on a Wizard form?

When user changes parent form status to 'Closed - Resolved' with a datatable status still 'Open', they're allowed to save and close the wizard form. I sent a PM of the console errors. Thanks.

@ParAvion,

Your code has multiple errors that are not related to the validation. Please comment out all code except the code that validates the Status field, to make sure it works.

Then, debug the rest of the code. You can find the instructions in this post: