I've got a document that needs to take different layouts based on the value in a specific dropdown field. These layouts are controlled by layers (OCGs). So I created a JavaScript function to do what I need. This function needs to be called on Document Open (because PDF apparently doesn't save layer visibility) and whenever the value in the dropdown box is changed.
I use the same script for both (checking for this.event.name === "Open" or this.event.willCommit). If the former is hit, I get the value of the dropdown box, skip all validations (assuming a saved document is in a valid state), and set the layer visibilities accordingly. Works fine.
If the latter is hit, though, I need to do some validations, because the layout change needs to be allowed/prohibited based on the contents of certain other fields. Think prohibiting changing to a layout that hides certain fields if those fields have values in them (that's not exactly what's going on, but close enough). The actual validation doesn't really matter because I have that part working too; it checks what it needs to check, and if it fails, provides a pop-up box letting you know what the problem is. (Interestingly enough, while that pop-up is displaying, the dropdown box shows the old value.)
My problem is that after the validation has failed, I can avoid making any changes to the layout (by skipping the section of the code that does the visibility changes), but I cannot find a good way to stop the update to the value in the dropdown box itself (which happens after the pop-up box is dismissed). For example, if the dropdown box is set to "Layout A", and I then change it to "Layout B" (which then fails the subsequent validation), the actual document layout will correctly remain "Layout A"--but the dropdown box will change to have "Layout B" as the value. If I then save and reload the document, then "Layout B" will take effect, because that was the value in the dropdown box when the document was saved (because it didn't revert).
What I need to know is, how do I stop it from updating the dropdown if the validation fails (or failing that, revert the dropdown value back to the original at the end of the Keystroke Action script)? I can use the following code to get the original (pre-changed) value of the field, and it works fine:
Code: Select all
let myFieldName = this.event.target.name;
let myField = this.getField(myFieldName);
let myOriginalValue = myField.value;
Code: Select all
this.event.change = myOriginalValue;
this.event.changeEx = myOriginalValue;
this.event.value = myOriginalValue;
myField.value = myOriginalValue;
Anyone have any suggestions on what might actually work for this?