Reset field attributes

In the editor, we can set attributes to input fields, like “required”. We can also do this in code. Now, I have a complex form with all fields, in editor, set to “not required”. Depending on a value from a dropdown, I set some fields to required. So if ddvalue = “a”, textfield1 and datePicker1 are required. If ddvalue = “b”, textfield2 and datePicker2 are required. But … the form stays resident, it is not reloaded when ddvalue changes. So in the above case, if we fill out form with ddvalue “a” and then with ddvalue “b”, ALL 4 fields are (erroneously) required, not only textfield2 and datePicker2.
I could solve this to look for all types of form.children, set all (4) fields first to “not required”, then set required according to value of dropdown. But isn’t there an easier way to reset an attribute to its initial (editor) value?

I’m not sure to understand your question? What’s the link between your conditional requires and the reset? Those are 2 differents features.

Apparently, you already figure out how to do the conditional require but what the relation between reset the value of a field and the optional required?

To do a reset feature store all of your value when you load the data then restore them on reset. Here an oversimplified example

let input1Default;
let input2Default;
let input3Default;

$w.onReady(async () => {
   data = await fetchData();
   
   input1Default = data.field1;
   input2Default = data.field2;
   input3Default = data.field3;
   
   $w("#resetButton").onClick(onReset);
   
   onReset(); //initialize value
})

function onReset(event) {
    $w("#input1").value = input1Default;
    $w("#input2").value = input2Default;
    $w("#input1").value = input3Default;
}

Is this what you are looking for or is there something I did not understand?

Quentin, thanks for taking the time to answer, but that I know, it is not the problem. Re " What’s the link between your conditional requires and the reset? ", the following: Wix has a bit of a quirk in its forms. If you put a input text box on a form, make it “required” and set the field to “hidden” or collapsed, your validation will prob say something like “Error: please fill out the required fields, marked with a red border”. But there will be no field with red border, because it is hidden/collapsed.
So, the form I have is pretty complex: under condition A boxes 1,2 and 3 should expand, under condition B only 1 and 2, and the “required” attribute varies per option.
The key point in my problem description is " But … the form stays resident, it is not reloaded when ddvalue changes. " Meaning: under condition 1 fields A and B are required, under condition 2 fields C and D. But, the form can be filled out endless amount of times. If you first fill it out under condition 1 and later under condition 2, ALL fields are mandatory (incorrect): condition 1 sets A and B as required, condition 2 sets C and D as required, but A and B are not reset, so all 4 are required (and A and B are prob. hidden, triggering the problem I started out with). So before you accept a new state (1 or 2), you first have to reset all values to non-required/enabled/checked/collapsed (like they are set in the editor), then make some fields required/disabled/unchecked/expanded. This, to overcome the problem of “accumulation of required (and other attribs) fields”.
So I can iterate thru all children, check its type and reset it, but now I am defining its initial “blank as a baby bottom”'s state twice: once in the editor and once in code.
So I was just wondering if there was some way to retrieve the initial attributes per field as set in the Editor, to prevent from doing it twice.
In short, I am looking for a) reset field attribs to Editor values or b) a NO_VALIDATE param to collapse/hide to prevent the problem I described at the top: if a field is required BUT it is hidden or collapsed or its parent is, then do not trigger validation.
It’s late here, to me it makes perfect sense now, and I can only hope it does to you too. If not, do not hesitate to let me know. Good night/good morning.

So, do you already change the required property of your fields? required is read & write

that means you can do something like

setFormCondition(conditionNumber) {
   if(conditionNumber === 1) {
      $w("inputA").required = true;
      $w("inputB").required = true;
      $w("inputC").required = false;
      $w("inputD").required = false;
   } else {
      $w("inputA").required = false;
      $w("inputB").required = false;
      $w("inputC").required = true;
      $w("inputD").required = true;
   }
}

This will help in the case the element is hidden and has no value. (other validators won’t run on empty fields)

If this does not help, do you mind sharing your code or some simplified version so we have better understanding :slight_smile:

Quentin, thanks, but I did it differently. Obviously, I cannot retrieve the initial state as set in Editor, so I defined the fields in code too. But because they are many, I didn’t want to write it out in full. So I dumped all required fields into an array and toggle required-attrib in a loop, depending on status. A lot shorted and as an added bonus, I can check validity the same way: loop thru the array.
Downside is that I have to remember to add a fields also to the array if I add a required field in form.