Skip to content
New issue

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

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

Already on GitHub? # to your account

templates: allow displaying dynamic error message on forms created via Form Builder plugin #11275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tajmirul
Copy link

@Tajmirul Tajmirul commented Feb 19, 2025

Close #11274

Why this PR?

I've created a custom phone number input block for my form builder, including validation. However, the component on the frontend only displays the generic message "This field is required," even when formState.errors contains specific error messages. This is not the expected behavior. I need the component to display the error messages from formState.errors.

Description

This pull request includes changes to improve error handling in various form components by passing the name prop to the Error component and updating the Error component to display specific error messages.

Error handling improvements:

Form component updates:

@Tajmirul Tajmirul changed the title fix(form-builder): Dynamic error message for form builder input fix(plugin-form-builder): Dynamic error message for form builder input Feb 19, 2025
@Tajmirul Tajmirul changed the title fix(plugin-form-builder): Dynamic error message for form builder input fix(plugin-form-builder): dynamic error message for form builder input Feb 19, 2025
@paulpopus paulpopus changed the title fix(plugin-form-builder): dynamic error message for form builder input templates: allow displaying dynamic error message on forms created via Form Builder plugin Feb 19, 2025
@paulpopus
Copy link
Contributor

Hey, thanks for your PR, can you give me a config I can test this with please?

@Tajmirul
Copy link
Author

Tajmirul commented Feb 25, 2025

Hi,

Apologies for the delayed response. I've created a Git repository demonstrating a custom phone number input block within a form builder. This allows rendering a phone input field on the frontend. I've also implemented error handling to display the appropriate message ("Invalid phone number") when necessary. This is achieved by passing the input's "name" attribute to the component, which then retrieves the corresponding error from the formState.

It's important to note that the current implementation of custom inputs in the form builder is a temporary workaround. This is not a part of the pr. The approach used is illustrated in this screenshot: image

image

To see the changes in action, please follow these steps:

  1. Clone and run the repository: https://github.com/Tajmirul/payload-form-builder
  2. Create a new form and add the "Phone Field" block.
  3. Create a new page.
  4. Add a "Form Block" to the page and select the form you just created.
  5. Save the page and visit it.
  6. Enter an invalid phone number and click the submit button.

You should now see the error message "Invalid phone number," which is the desired behavior. Previously, the error message was a generic "This field is required."

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error message is fixed "This field is required" in form builder
2 participants