Hello to all!
I have a (form/fields) and a (submit button), I just added wixLocation.to(“/thanks”); to the code like this:
export function Submitbutton_click(event) {
let C6stext = $w("#text82").text;
$w("#AssessorInputdataset").setFieldValue("c6SmallText", C6stext);
$w("#AssessorInputdataset").save();
//Move to thank you page
wixLocation.to("/thanks");
}
It works good, but it passes the (form/fields) validation. if I clicked on the button it goes to the other page even if the form is not submitted. I just want it happens, if the (form/fields) are submitted correctly and valid.
I will keep trying to find a solution. Thanks in advance!
Hello Samy,
i can not see your validation-process?
For example where is your IF-QUERY ???
if () { }
else { }
???
Also take a look at …
This is what I need but I don’t know how to do it.
if (Form or Submit Button is valid) { wixLocation.to(“/thanks”); }
else { Do Nothing }
Well because you are working with wix.locations, so first at all you should import the “wix.location-API”
https://www.wix.com/corvid/reference/wix-location#top
import wixLocation from'wix-location';
Then the onReady-Command follows…
onReady(function(){ })
And then the rest of your code…
export function Submitbutton_click(event) {
let C6stext = $w("#text82").text;
let isValid = $w("#myElement").valid;
//starting the if-query.....
if (isValid) {
//save-process
$w("#AssessorInputdataset").setFieldValue("c6SmallText",C6stext)
$w("#AssessorInputdataset").save();
//redirection-process
wixLocation.to("/thanks");
}
else { //here will happen nothing }
}
Something like this 
Curious …So why are you not connecting the input elments to a dataset and setting input elements to required via their settings?
If you do this and connect the button to the dataset as well … you do not need to write code.
Ahhh, I think you are setting a text value as a value in your dataset. I see now.
Well you have a couple of options to change it …
(1) On page ready, on dataset ready, set the value of the field but remove the ‘save’ function. Just use the setField function.
(2) Use onBeforeSave to setField value. Then onAfterSave to redirect.
Whichever way you choose, you can easily connect ALL form input elements to the dataset and use their settings to mark them as required. Then you can connect button to dataset.
Once the button is connected to the dataset you may not need ro use the onAfterSave function as you can use the button settings to select a page to redirect to.
(If you choose none of the above you will have add code manually to check if all inputs are valid onBeforeSave. You can still trigger your function via button click but you would have to restructure your code to something like: on click check if all these fields are valid, if yes, save, then redirect to this location. Right now you are not using .then in your code)
Links
https://www.wix.com/corvid/reference/wix-dataset/dataset/onbeforesave
https://www.wix.com/corvid/reference/wix-dataset/dataset/onaftersave
https://www.wix.com/corvid/reference/$w/textinput/valid