lwc icon indicating copy to clipboard operation
lwc copied to clipboard

lightning-datatable inline editing required attribute

Open NichoBridge214 opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. Feels like we should be able to add the required attribute to fields in a lightning-datatable

Describe the solution you'd like under cell attributes allow for the the required attribute to be applied for better/easier client side validation on fields during inline editing

Describe alternatives you've considered This solutions is out there anytime i've searched this feature but seems this solution should be included or made easier to add a required attribute:

There are a few scenarios you should consider when requiring / validating entry in a lightning-datatable.

Example Required Field in LWC Lightning Datatable

A custom "required input cell" with an asterisk on edit would only be possible if you create a custom data type, see the following resources:

[LWC Datatable: See section 'Creating Custom Data Types '](https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation)
[SLDS Data Table w/ Required Error](https://www.lightningdesignsystem.com/components/data-tables/#Required-form-element-with-error)
[How to use custom LWC lightning component in lightning-datatable](https://salesforce.stackexchange.com/questions/271111/how-to-use-custom-lwc-lightning-component-in-lightning-datatable/271112#271112)
OOTB - Required Field
If the field is configured, out of the box (OOTB), as required, then an error on the cell will display after you attempt a save. The DML operation will fail, and the standard table error will be returned: [LWC Datatable: See section 'Displaying Errors'](https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation)

OOTB - Apex (Triggers) / Validation Rules
When an error is added to a record during a DML operation, via configuration or Apex code, then those errors will propagate back to the LWC datatable as error messages at the table and row levels -- see above documentation 'Displaying Errors'.

Client side validation
If none of the above are configured in your org, then you can create an onsave handler in your JavaScript, check the validity of the table rows (both draftValues & existing table data), and if validation fails populate an error object that you pass back to the table error attribute. See above documentation 'Working with Inline Editing'

NichoBridge214 avatar Aug 22 '22 14:08 NichoBridge214