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

Displayed validation error gets stuck #6413

Open
benjaffe opened this issue May 17, 2024 · 1 comment
Open

Displayed validation error gets stuck #6413

benjaffe opened this issue May 17, 2024 · 1 comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@benjaffe
Copy link

benjaffe commented May 17, 2024

Link to reproduction

No response

Describe the Bug

When the validate function returns an error, it gets stuck showing that first error, even if the user types new content. This is misleading when the user fixes one issue but has another issue that the validation function is catching.

To Reproduce

Set up an input like this and publish to trigger validation on typing. Then type 12345 in the input. When you get to 3, it shows Error number 3 as expected. But then typing subsequent characters doesn't update the displayed error, even though the logs show the validate function returns different errors.

{
  name: `test`,
  label: `Test`,
  type: 'text',
  validate: async (data: string) => {
    if (!data || data.length < 3) {
      return true;
    }
    const errStr = `Error number ${data.length} `;
    console.log(errStr);
    return errStr;
  },
},

Payload Version

2.16.1

Adapters and Plugins

@payloadcms/richtext-slate 1.5.2

@benjaffe benjaffe added the status: needs-triage Possible bug which hasn't been reproduced yet label May 17, 2024
@benjaffe
Copy link
Author

benjaffe commented May 17, 2024

I can get around this by watching my inputs and manually calling validateForm each time, though this isn't very intuitive as a solution. More context in this discord thread.

function startValidation(formHookResult: ReturnType<typeof useForm>) {
  const { formRef, setSubmitted, validateForm } = formHookResult;
  setSubmitted(true);
  // make sure we have the monaco editor textarea element on the page, and add an event listener
  if (formRef.current) {
    const inputFields = Object.values(formRef.current).filter(
      (el) => el.id === 'field-subject' || el.className === 'inputarea monaco-mouse-cursor-text',
    );
    // if we have inputs, add an event listener on input and call validateForm manually
    if (inputFields.length) {
      inputFields.forEach((field) => {
        // we call validateForm in the next tick so the value has been updated
        field.addEventListener('input', () => setTimeout(() => void validateForm()));
      });
    }
  }
  // if the above failed, do it again in a second
  setTimeout(() => startValidation(formHookResult), 1000);
}

and

// This is an invisible element that exists only to trigger the form's validate-upon-input behavior.
{
  type: 'ui',
  name: 'form-validation-trigger',
  admin: {
    components: {
      Field: () => {
        // `setSubmitted` seems to mainly be for handling the validation behavior of the form on input.
        // By setting to true, the form will validate the fields on input, which is the behavior we want.
        // Otherwise, it only validates on input after clicking "Publish" (Save Draft doesn't trigger validation)
        const formHookResult = useForm();
        startValidation(formHookResult);
        return null;
      },
    },
  },
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

1 participant