Hello, I am trying to submit a form but I don’t want a user to be allowed to submit his/her data if it already exists in the database and its “status field” is not set to 1. I’m using this code:
import wixData from 'wix-data'; export function button1_click(event, $w) { wixData.query("CancellationRequests") .eq("ORDERID", $w('#input2').value) .find() .then(res => { if (res.length > 0) { //show error message - existing cancelation form } else { $w('#dataset1').save() //form submitted - action succeeded .then(console.log("save done")) } }) }
What I am trying to do is this:
- When the user submits his/her data it should be checked if the value of the OrderID field (which is unique, has to be) matches with any values in the database’s OrderID column. If it does not match then the form is submitted otherwise its not.
- Even if there’s a similar OrderID there is a Status field which shows whether that request has been processed or not. (Processed- 1, Not Processed-0). A request with a duplicate OrderID should only get submitted if the Status field is 1.
Any help with the code?
Hi Vipul -
Maybe try the following code:
import wixData from ‘wix-data’;
export function button1_click(event, $w) {
wixData.query(“CancellationRequests”)
.eq(“ORDERID”, $w(‘#input2’).value)
. eq(“refundReplace”, 0)
.find()
.then(res => {
if (res.length > 0)
{ //show error message - existing cancelation form }
else
{ $w(‘#dataset1’).save() //form submitted - action succeeded
.then(console.log(“save done”))
}
})
}
Note, make sure that the “ORDERID” case matches the actual Field’s key value case.
Best,
Nick
Hello Nick,
Thanks for the solution but I tried and it is the same. No luck. The form is still getting submitted. I think it is because the submit button is connected to the dataset for submitting the data itself. Therefore until somehow the process of submission is stopped this won’t work. Any workarounds?
Best,
Vipul
Try disconnecting the button from the dataset (making it a standard button) and on the onClick do all your checks and, if it needs to be saved, do a .save() on the dataset. This will work.
Hello Vipul,
I would try what Giri suggested by disconnecting the button from the Dataset. Then retry submitting using the above button click event code.
Hope this helps.
Hi Nick,
Your solution worked. Cheers! 
Great, glad to hear Vipul.
Nick