Hello everyone, always with the intention of learning javascript I’m trying to save the data inserted in a collection. Before doing this, however, I want to check that the fields entered are valid according to the type of data chosen.
I can not understand why you give me error.
ImputValid ();
IF (validationForm === true) {
this is the complete code
//FUCTION FOR SEE IF FORM IS VALID
function ImputValid() {
let ImputValid_01 = $w(“#ImptData”).valid;
let ImputValid_02 = $w(“#ImptEvent”).valid
let ImputValid_03 = $w(“#ImputName”).valid
let ImputValid_04 = $w(“#ImptCode”).valid
let ImputValid_05 = $w(“#ImpuUser”).valid
let ImputValid_06 = $w(“#ImputUrl”).valid
if ( (ImputValid_01===true) && (ImputValid_02===true) && (ImputValid_03===true) && (ImputValid_04===true) && (ImputValid_05===true) && (ImputValid_06===true)){
let validationForm = true;
} else{
let validationForm = false;
}
}
//INSERT IN TO DATABASE
export function SaveData_click() {
let toInsert = {
“dataEvento”: $w(“#ImptData”).value,
“tipoEvento”: $w(“#ImptEvent”).value,
“nomeEventoOErimonia”: $w(“#ImputName”).value,
“code”: $w(“#ImptCode”).value,
“utenteRedirect”: $w(“#ImpuUser”).value,
“redirect”: $w(“#ImputUrl”).value
};
ImputValid();
IF (validationForm === true){
wixData.insert("RedirectCerimonie", toInsert)
.then( (results) => {
//let item = results;
$w('#TabEventi').refresh();
//clearImput();
//reloadTable();
$w("#textSaveForm").text = "Form Save";
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
} else {
$w("#textSaveForm").text = "Form Error";
}
}
Eventually there are instructions to facilitate this task
Thank you
Hi,
I see a couple of things:
ImputValid ();
IF (validationForm === true) {
-
validationForm is not declared (it’s declared later in your code, but isn’t available for the above code)
-
IF should be if
What error (or errors) are you getting?
Yisrael
Hello Thanks for your reply,
actually gives me the error that the variable validationForm is undefine. How can I solve the problem?
I do not know how to return the value obtained from a function, as long as it can be done
Thank you
To return the value, just add the following line to the end of the InputValid() function:
return validationForm;
Then, you can use the value like this:
if (ImputValid() === true) {
You can read more about a returning a value in a function.
Good luck,
Yisrael
I tried to change the code as per your example, but it does not work, when I add return at the end of the function it always gives me the variable and undefine.
I tried in other ways looking at the guide but I could not
At the end, thanks to the guide I solved by doing a
return true;
ritun false;
directly from the function without using the variable
Thank you
Great! You return true; and return false: is perfect.
I messed up in my answer to you regarding the declaration of validationForm .
Let me explain with a simple example:
function ImputValid() {
let ImputValid_01 = $w("#ImptData").valid;
if ( (ImputValid_01 === true) {
// validationForm declared here and only available inside { }
let validationForm = true;
} else {
// another different validationForm is declared here
let validationForm = false;
}
// we can't "see" either of the validationForm declarations above
return validationForm; // this does NOT work
}
This is what you need:
function ImputValid() {
let ImputValid_01 = $w("#ImptData").valid;
let validationForm; // declare validationForm
if ( (ImputValid_01 === true) {
// do NOT declare validationForm here (no let)
validationForm = true;
} else {
// do NOT declare validationForm here (no let)
validationForm = false;
}
// we can now "see" declaration of validationForm above
return validationForm; // this does NOT work
}
See more on let, including a playground to try it out.
I hope this helps.
Yisrael
Ok but if I do like your example if it should be set like this
ImputValid ();
IF (validationForm === true) {
and not so
if (ImputValid () === true) {
Is what I’m saying correct?
Thank you
Since in my example, the ImputValid() function returns a value, it can be used like this:
if (ImputValid () === true) {
or like this:
let validValue = ImputValid();
if (validValue === true) {
Both ways are equivalent. The function ImputValid() returns a value, and can be used directly (first example), or can be assigned to a variable (second example).
Thank you so much for the explanation,
I am still testing to learn this better.
Thank you and have a good day
Good job! I think you’ll get a lot out of the investement that you’re making learning this material.
Remember me when you’re rich and famous. 
Surely I will remember you, they are all information that opens up a world that fascinates me.
Thank you so much