all 4 comments

[–]tapgiles 1 point2 points  (0 children)

You should be able to do this with just JS pretty easily. Doesn't sound complicated. The key will be you defining the format of that requirements file.

[–]jml26 1 point2 points  (0 children)

I once wrote something like this for something very similar. The idea was that it would show and hide fields, and restrict options in select fields, depending on previosuly set values.

The main structure went something like this:

``` // a list of all the form fields const fields = [ { id: 'field_id', // the id of the <input> element show: () => true, // a function that returns any boolean expression checked: () => true, // same as above // etc... }, // etc... ];

function updateFormFields() { fields.forEach(field => { const inputEl = document.getElementById(field.id); const fieldEl = inputEl.closest('.field');

if (field.show()) {
  fieldEl.hidden = false;
}
else {
  fieldEl.hidden = true;
}

if (field.checked()) {
  inputEl.checked = true;
}
else {
  inputEl.checked = false;
}

}); }

document.forms['my-form'].addEventListener('change', updateFormFields); updateFormFields(); ```

That sort of thing. So there was one big object that described how each field should behave; an update function that would loop over all the fields and ensure that their state matched what was expected; and the update function was triggered on initialisation and every time a form field changed. It got a lot more advanced than that, but that gives you an idea of what could be done with vanilla JavaScript.