Dear All,
I have created a student admission form there are some automatic calculation fields. When I try to submit the form, all the fields are saved in the database except the automatic calculation fields. I want to know how to save those automatic calculation fields in the database.
Please help me.
Thanks
If your calculations are done in the ‘Submit’ event handler, I have the same problem.
Hi @sumankht ,
I recommend addign a query check in the field’s valudations, but only run the validation when the whole number is entered, for example, if the registration number is 8 digits, run the query if it’s 8, and reject it if otherwise, here’s an example.
import { checkRegNo } from 'backend/<students.jsw>';
import { query } from 'wix-data';
Make sure to replace “<students.jsw”> with your BE web module name, and the “checkRegNo” function with your own function.
For security reasons, never run any query on the FE.
const reg_num_length = 8;
let processing = false;
$w('#regNo').onCustomValidation((value, reject) => {
const length = value.length;
if (length < reg_num_length) {
if (length === 0) {
return reject('Please enter your registration number.');
}
return reject(`The registration no is ${reg_num_length} characters, ${reg_num_length - length} characters left`);
}
if (length === reg_num_length) {
// Run the query now, if no matches returned, reject the value.
return new Promise((resolve, reject) => {
// Start the checking processing;
processing = true;
// Run the check on the backend
checkRegNo(value).then((regNo) => {
regNo.valid ? resolve() : reject();
})
}).then(() => {
// The reg. no is valid, do nothing.
}).catch(e => {
// The reg. no is incorrect or is invalid, reject the value
reject('The registration number is invalid');
}).finally(() => {
// Stop the processing;
processing = false;
})
}
})
Keep in mind, that during the validation (on the backend) process, the field value is considred “VALID”, that’s why I introduced a Boolean to indicate whether we’re checking or not, you need to disable your submit button when the field is being checked, if you’re using a dataset, you can set an onBeforeSave( ) event handler to reject the registration if the field is still checking, here’s a quick example as well.
$w('#dataset').onBeforeSave(() => {
// Abort the saving process if the field is still checking
if (processing) { return false }
})
Hope this helps~!
Ahmad
Thanks @ Ahmad for your valuable feedback…
@murphyjaneway ,
No, I am changing the calculation value by using the change event of the input box & dropdown.
@sumankht you’re welcome
Right. Thanks, Suman