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;
}
})
}
stopPropagation
andpreventDefault
should be outside of theUser()
thingy altogether. Inside ofthen()
is too late.stopPropagation
andpreventDefault
outsideUser().then()
but still not working and it didn't even show the alert.