1

As per below salesforce documentation we can display error for lightning datatable by setting the "error" attribute.

https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

triggerError: function (cmp, event, helper) {
    cmp.set('v.errors', {
        rows: {
            b: {
                title: 'We found 2 errors.',
                messages: [
                    'Enter a valid amount.',
                    'Verify the email address and try again.'
                ],
                fieldNames: ['amount', 'contact']
            }
        },
        table: {
            title: 'Your entry cannot be saved. Fix the errors and try again.',
            messages: [
                'Row 2 amount must be number',
                'Row 2 email is invalid'
            ]
        }
    });
}

the above example is for showing one error. Since lightning:datatable has multi-edit functionality, a user can try to edit multiple rows at the same time.

is there any way to display the error for all the edited rows?

I tried to do something like below, but I am getting compilation error.

component.set('v.errors', {
                rows: [{
                    a: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                table: {
                    title: 'Your entry cannot be saved. Fix the errors and try again.',
                    messages: [
                        'Row 2 amount must be number',
                        'Row 2 email is invalid'
                    ]
                },
                {
                    b: {
                        title: 'We found 2 errors.',
                        messages: [
                            'Enter a valid amount.',
                            'Verify the email address and try again.'
                        ],
                        fieldNames: ['estDebt', 'responsibility']
                    }
                },
                table: {
                    title: 'Your entry cannot be saved. Fix the errors and try again.',
                messages: [
                    'Row 2 amount must be number',
                    'Row 2 email is invalid'
                ]
            }]
        });
2

1 Answer 1

3

Your format is messed up. There should be two top-level objects, rows and table. The rows key should contain keys that match the keyField values for the table. Table messages themselves should be in the table key.

{ rows: 
  { a: { title: "...", messages: [...], fieldNames: [...] }, 
    b: { title: "...", messages: [...], fieldNames: [...] }
  },
  table: { title: "...", messages: [...] }
}

Assuming you had a list of errors from the server, you might do something like this:

var errors = { rows: {}, table: {} }
errorsFromServer.forEach(
  error => errors.rows[error.Id] = { title: "...", messages: [...], fieldNames: [...] }
);
errors.table.title = "We found "+errorsFromServer.length+" error(s). ...";
errors.table.messages = [...];
cmp.set("v.errors", errors);
3
  • Thanks! it is helpful. By any chance can we use them with for loop? I can store the error attributes in List/Map in the controller, but I want to iterate the error attributes with for loop instead of hardcoding them. Commented Jan 7, 2019 at 19:28
  • @SFDCGeek Updated answer based on your comment. Does this help?
    – sfdcfox
    Commented Jan 7, 2019 at 19:42
  • interestingly, I did the exact same thing mentioned in the docs, but still not able to show the error messages. triggerError(event) { this.errors = { rows: { a: { title: '..', messages: [ '...' ], fieldNames: ['..'] } }, table: { title: '...', messages: [ '..' ] } }; } Commented Aug 30, 2023 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .