Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How we can validate the required fields? #33

Closed
jkyadav opened this issue Mar 28, 2022 · 2 comments
Closed

How we can validate the required fields? #33

jkyadav opened this issue Mar 28, 2022 · 2 comments

Comments

@jkyadav
Copy link

jkyadav commented Mar 28, 2022

I want to validate and make sure that end user filled in all the required fields upon clicking Complete button.

I could not find that code in your repo. could you put some light on this please?
image

Thanks

@CorreyL
Copy link
Contributor

CorreyL commented Mar 29, 2022

Hello @jkyadav,

Thanks for reaching out! Happy to assist you with this question.

The first place I would reference is our guide here on how to iterate through all the fields in a PDF:

https://www.pdftron.com/documentation/web/guides/forms/modify-fields/#iterate-over-all-fields

And to check if all Required Fields have a non-empty value inside of them, I would do something like this:

WebViewer(/* ... */).then(instance => {
  const { documentViewer, annotationManager, Annotations } = instance.Core;

  const checkIfAllRequiredFieldsAreFilled = (field) => {
    const { value, type, widgets } = field;
    if (type === 'Sig' && widgets[0].annot) {
      // Logic for when a required Signature field has not been filled goes here
    }
    if (
      field.flags.get(Annotations.WidgetFlags.REQUIRED)
      && (
        // For Textboxes, Comboboxes, Listboxes
        value === ''
        // For Checkboxes or Radiobuttons
        || value === 'Off'
      )
    ) {
      // Logic for when a required field has not been filled goes here
    }
    // Check children fields
    field.children.forEach(checkIfAllRequiredFieldsAreFilled);
  }

  documentViewer.addEventListener('annotationsLoaded', () => {
    // Please ensure the annotationsLoaded event has fired before invoking the
    // checkIfAllRequiredFieldsAreFilled method like below
    const fieldManager = annotationManager.getFieldManager();
    fieldManager.forEachField(checkIfAllRequiredFieldsAreFilled);
  });
});

I hope this helps, but please feel free to let me know if you have any follow-up questions!

@jkyadav
Copy link
Author

jkyadav commented Mar 30, 2022

@CorreyL Yes, it worked, thank you.

@jkyadav jkyadav closed this as completed Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants