Fix errors with unsupported form field actions
When modifying PDF form fields in Nutrient Web SDK, you may encounter errors if the fields contain unsupported JavaScript actions. This guide explains how to identify and resolve such errors.
The problem
Attempting to modify form fields that contain unsupported JavaScript actions, such as onBlur, can result in the following errors:
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'script')Uncaught (in promise) PSPDFKitError: Assertion failed: JavaScriptAction requires scriptUnderstanding action support
Form field objects and widget annotations support different sets of actions:
- Form field objects — Support a limited set of actions. For the complete list, refer to the FormFieldAdditionalActions API reference.
- Widget annotations — Support a wider range of actions, including
onBlur. For the complete list, refer to the WidgetAnnotationAdditionalActions API reference.
Solution
To modify form fields with unsupported actions, first remove these actions, and then apply your desired changes:
NutrientViewer.load({ ...baseOptions, theme: NutrientViewer.Theme.DARK,}).then(async (instance) => { const formFields = await instance.getFormFields(); // Identify form fields with onBlur actions and remove only that action. const fieldsWithOnBlur = formFields .filter((formField) => { return formField?.additionalActions?.onBlur; }) .map((formField) => formField.set("additionalActions", formField.additionalActions.delete("onBlur"))); try { // Remove the onBlur actions. await instance.update(fieldsWithOnBlur); // Apply your desired changes (for example, setting fields to read-only). await instance.update( (await instance.getFormFields()).map((formField) => formField.set("readOnly", true) ) ); } catch (error) { console.log(error); }});This approach:
- Identifies form fields with
onBluractions. - Surgically removes only the
onBluraction while preserving other supported actions. - Applies the desired changes to the form fields.
- Uses a
try/catchblock to handle any remaining errors.
Important considerations
- Form field objects don't support the
onBluraction, but widget annotations do. - For PDFs with many form fields or complex embedded JavaScript, some errors may still occur when rendering new pages. If this happens, contact our support team(opens in a new tab) with the specific error messages and a sample PDF if possible.
- The
try/catchblock ensures your code continues to run even if some errors occur.
For more information about JavaScript actions support in Nutrient Web SDK, refer to the PDF actions support guide.