Wix Forms: OnWixFormSubmitHookError => changes to fields are not valid

Hi!
In my Wix Form, I have a dropdown (selection field). The options are populated from a collection such that the option 's label is a readable text (“ABC Course Name”) and its value is a system generated ID (" aca0f0a2-aa6a-4474-b082-5f8990e3c8bb "). When the form is submitted, the email notification sent contains the form field Name and Value pairs. As the value is something unreadable like mentioned, I want to set it to the Label Text. I do this change in the onWixFormSubmit event handler. But it gives the above error, whereas the fieldValue change to a text input field is accepted. Kindly help, i am not able to locate the problem.

$w("#wixForms1").onWixFormSubmit((event) => {

    let index = $w("#dropdownCourseName").selectedIndex;
    let dropdownoptions = $w("#dropdownCourseName").options;
    let coursename = dropdownoptions[index].label;
  
    for (var i=0; i < event.fields.length; i++) {
          console.log ("Event field", event.fields[i]);
// dropdown selection field element. Value is set to Text.  fieldValue change gives error
         if (event.fields[i].id === "dropdownCourseName"){
             event.fields[i].fieldValue= coursename;
         }
// text field element. Value is set to Text. No error. fieldValue change is accepted
      if (event.fields[i].id === "input25"){
             event.fields[i].fieldValue= "FIRST NAME";
              
         }
    }

    return event.fields;
  })  

Thanks.
Gita

Hi,
Try yo change one value only and see if it works.
Try to console.log() the coursename and see what it logs.
Try to console.log() filed value after it changes and see what it logs.

HI JD!
Thanks for the reply.

  1. Initially I had tried only one field, the dropdown fieldValue change. The value changed. But the error is thrown while returning or outside the event handler, I suppose.
    (I have added a lot of console log stmts which I removed in the post to the make the code small :grinning:)
  let coursename = dropdownoptions[index].label ; // set value to readable Name

  event.fields[i].fieldValue = coursename;   // Error
  //Wix Forms: OnWixFormSubmitHookError => changes to fields are not valid
  1. So I tried a field value change for another basic type which is a text field, and it worked.
let coursename = "FIRST NAME";    // Another field (input25) type Text
if (event.fields[i].id === "input25"){
             event.fields[i].fieldValue = coursename;   // FieldValue Changes - Works
         }

3)I set the dropdown fieldValue to “”. It works. No error, the form submission mail shows Cours Name: -


  event.fields[i].fieldValue = "";  // FieldValue Changes - Works
  1. I set it the original value again. It works. No error.
let coursename = dropdownoptions[index].value ; // set coursename to option's value

  event.fields[i].fieldValue = coursename;    // FieldValue Changes - Works
    1. I set it another option’s value. It works. No error.
let coursename = dropdownoptions[index+1].value ; // set coursename to next option's value in the dropdown

  event.fields[i].fieldValue = coursename;    // FieldValue Changes - Works
  1. When I set it to just " ", (a space), error is thrown.

  event.fields[i].fieldValue = " "; // Error
//Wix Forms: OnWixFormSubmitHookError => changes to fields are not valid

Looks like, though the dropdown value is a string, it does not accept any value other than one of the values of its options or undefined “”.

Pls throw some light and suggest a work around.

Thanks
Gita

@gitaganapathy Did you try a console.log right after getting the coursename ? What value are you getting? It sounds as if the error is being thrown due to an invalid value.

let coursename = dropdownoptions[index].label;
console.log('course name', coursename); // what do you get here?

@yisrael-wix
This my code with console.log

   $w("#wixForms1").onWixFormSubmit((event) => {
    console.log("Inside on submit");
    console.log ("On Entry - ", event.fields);
    let index = $w("#dropdownCourseName").selectedIndex;
    let dropdownoptions = $w("#dropdownCourseName").options;
    console.log ("Dropdown Label :  ", dropdownoptions[index].label," Value: ", dropdownoptions[index].value);
    let coursename = dropdownoptions[index].label;    // Set to readable label 
    // let coursename = "FIRST NAME"                    // To Set another text field 
    // let coursename = dropdownoptions[index].value;     // Set to original unreadable options value
    //      let coursename = dropdownoptions[index+1].value; // set to another unreadble options value

    console.log ("Coursename ", coursename);
   // console.log( "Length", event.fields.length);
    for (var i=0; i < event.fields.length; i++) {
   //       console.log ("Event field", event.fields[i]);
         if (event.fields[i].id === "dropdownCourseName"){
           console.log ("Event field", event.fields[i]);
           event.fields[i].fieldValue = coursename;
    //        event.fields[i].fieldValue = "";          // change fieldValue to ""
    //        event.fields[i].fieldValue = " ";         // Change fieldValue to " "
         }
         /** 
      if (event.fields[i].id === "input25"){
             event.fields[i].fieldValue = coursename;
           
         }
           **/ 
    }
       console.log ("On Exit - ", event.fields);
    return event.fields;
  })  

This is the console.log messages


This is expanded On Entry console.log

This is the expanded On Exit console.log


The form submission message:

Hi!
Added a few more console.log to track the fieldValue before and after changing: Also another text field to track the changes. I guess it looks like synchronisation issue. According to velo reference:


The new console message:

But the form submission message is different:


Kindly help to solve.
Thanks
Gita

@gitaganapathy Thanks for the logs…

If I understand correctly, you are actually trying to change the value of the dropdown which (from the error message) might not be permitted.

I admit that Wix Forms is not exactly in my wheelhouse. It could be a bug in which case I would suggest contacting Wix Customer Care regarding this issue. They will be able to better evaluate your issue.

@gitaganapathy I don’t see where the issue would be due to synchronicity as you are not calling any async code which would be the cause of the problem. Your code is all inline synchronous code.

Forgot to ask…

Where do you se t the onWixFormSubmit() function? It should be in the page’s onReady().

Do you have any code in the onClick() event handler of the form’s Submit button?

Yes. it is inside onReady,


   $w("#wixForms1").onWixFormSubmit((event) => {
    console.log("Inside on submit");
    console.log ("On Entry - ", event.fields);
    let index = $w("#dropdownCourseName").selectedIndex;
    let dropdownoptions = $w("#dropdownCourseName").options;
    console.log ("Dropdown Label :  ", dropdownoptions[index].label," Value: ", dropdownoptions[index].value);
    let coursename = dropdownoptions[index].label;    // Set to readable label 
    // let coursename = "FIRST NAME"                    // To Set another text field 
    // let coursename = dropdownoptions[index].value;     // Set to original unreadable options value
    //      let coursename = dropdownoptions[index+1].value; // set to another unreadble options value

    console.log ("Coursename ", coursename);
   // console.log( "Length", event.fields.length);
    for (var i=0; i < event.fields.length; i++) {
   //       console.log ("Event field", event.fields[i]);
         if (event.fields[i].id === "dropdownCourseName"){
           console.log ("Event field before", i, " ",event.fields[i]);
           event.fields[i].fieldValue = coursename;
    //      event.fields[i].fieldValue = "";          // change fieldValue to ""
    //        event.fields[i].fieldValue = " ";         // Change fieldValue to " "
          console.log ("Event field after", i, " ",event.fields[i]);
         }
         
      if (event.fields[i].id === "input25"){
        console.log ("Event Text field before", i, " ",event.fields[i]);
             event.fields[i].fieldValue = coursename;
         console.log ("Event Text field after", i, " ",event.fields[i]);  
         }
           
    }
       console.log ("On Exit - ", event.fields);
    return event.fields;
  })  
});
// End of onReady 

No onClick code for Submit button

Only a console.log onwixFormSubmitted() event handler

export function wixForms1_wixFormSubmitted() {
  // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
  // Add your code for this event here: 
  console.log ("Form Submitted");
}


@gitaganapathy As I said, maybe it’s just something that I don’t understand, but… It might be that there’s a problem changing the value of the dropdown. Either it’s a bug or a “feature” - that is, the expected/desired behavior. If it’s a bug, it needs to be fixed; If it’s the expected behavior, then the docs need to be fixed.

As I mentioned before, I would suggest contacting Wix Customer Care as they are smarter than I.

If you don’t want to spend too much time figuring it out, maybe you can try to do the value alterations in a beforeInsert data hook .

Thanks J.D.
Will try out beforeInsert data hook.
-Gita