0

I created datatable with onrowselection and after selecting a row the details will pass to a record form for editing. Now I want to prevent the record form from submitting in a certain condition. I tried event.stopPropagation() and event.preventDefault() but it doesn't work or I used them incorrectly. Does anyone know a way for this to happen? I appreciate any help.

HTML:

<template if:true={showModel}>
            <lightning-tab label="Car Model" onactive={handleModelData}>
                <lightning-input placeholder="Search Model" onkeyup={handleModelKeys}>
                </lightning-input><br/>  

                <div style="height: 15rem;">
                    <lightning-datatable key-field="id" data={modelRecords} columns={CarModelColumns} 
                        onrowselection={getSelectedModel}>
                    </lightning-datatable><br>
                </div><br>
                <lightning-tabset variant="scope">
                    <template if:true={showCarModelForm}>
                        <lightning-tab label="Create">
                            <lightning-record-form object-api-name="Car_Model__c" 
                                columns="2" layout-type="Full">
                            </lightning-record-form>
                        </lightning-tab>
                    </template>
                    <template if:true={showCarEditModelForm}>
                        <lightning-tab label="Edit">
                            <lightning-record-form object-api-name="Car_Model__c" 
                                columns="2" layout-type="Full" record-id={recordModelId} onsubmit={handleSubmit}>
                            </lightning-record-form>
                        </lightning-tab>
                    </template>
                </lightning-tabset>           
                
            </lightning-tab>
        </template>

JS:

recordModelId;
    getRow = [];
    getSelectedModel(event) {
        const selectedRows = event.detail.selectedRows;
        this.getRow = selectedRows;
        this.recordModelId = selectedRows[0].Id;
        this.showCarEditModelForm = true; 
        alert('Record: ' + this.getRow[0].Id + ' ' + this.getRow[0].Safety + ' ' + this.getRow[0].Quality);
        
    }
    handleSubmit(event){
        User().then(curUser => {
            if(curUser == 'Factory' && this.getRow[0].Stage != 'New'){
                event.stopPropagation();
                event.preventDefault();
                alert(this.getRow[0].Model + ' is currently/passed Manufactured Stage');
            } else {
                this.recordModelId = this.getRow[0].Id;
                this.showCarEditModelForm = true; 
            }
            if(curUser == 'Quality Analysts' && this.getRow[0].Safety == false && this.getRow[0].Quality == false){
                event.stopPropagation();
                event.preventDefault();
                alert(this.getRow[0].Model + ' Safety and Quality hasn\'t passed yet!');
            } else {                
                this.recordModelId = this.getRow[0].Id;
                this.showCarEditModelForm = true; 
            }
        })
    }
4
  • stopPropagation and preventDefault should be outside of the User() thingy altogether. Inside of then() is too late. Commented Jul 31, 2022 at 8:57
  • The other question would be, do you need to prevent records being updated with whatever condition you are looking for regardless as to whether it is done through your UI, a standard UI or even a REST or SOAP API? For me, most of the time you want data to be consistent and clean regardless of input mechanism, so this sort of thing is often best addressed via validation rules or other automation handled in the back-end.
    – Phil W
    Commented Jul 31, 2022 at 9:14
  • @PhilW I still learning in LWC and I want to test more things about it. Commented Jul 31, 2022 at 12:57
  • @FelixvanHove I tried to put the stopPropagation and preventDefault outside User().then() but still not working and it didn't even show the alert. Commented Jul 31, 2022 at 13:00

0

You must log in to answer this question.

Browse other questions tagged .