-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Form validation hook (to use outside of onSubmit) #1006
Comments
There is a |
@glasserc I got TypeError:
|
@xero88 I'm using the validate method on the form. Are you getting a ref to your form and calling validate on that? |
I am also running into this issue. Using a custom |
Please check this issue #500 , you will find examples of doing it. |
@doncesarts I think this is still a valid issue. Using the techniques outlined in #500, you're still triggering a submit, which just happens to validate. If validation passes, the form submits. That is not always desired, sometimes just a validation is required. Looking at the source code Thank you for your time. |
I still don't really understand the use case here. @kylehalleman Are you saying you don't actually use the The |
@kylehalleman what is your reason for not wanting to call validate on the form instance? Ultimately you could use ajv's validate method using your schema and form data. which is what the library does. I'm also interested in your use case for using SchemaField over Form. |
We use both the Specific to the need to validate without submitting, one of the custom widgets we created for editing a drop down list form field is the ability to add options to a In the scenario we've run into: A user enters two options in the array field, but they are duplicates and when hitting submit, gets validation errors for this. The user then switches to the "bulk editor" I can make a CodePen if that would help clear things up some more. However, I will try using the @llamamoray Thanks, I will look into using the ajv validate method directly if the above does not work. The reason I can't call validate on the form instance is where we would want to call the validate method is higher up in the component tree and even when rendering the edit: Re-reading the React docs on refs, I was unaware refs could be attached to the component instance and not just the underlying DOM element. Due to the component tree we have set up, I'm not entirely sure we'll be able to get a ref to the Thanks for your help, @glasserc and @llamamoray. |
Since I had to implement it semi-urgently, I ended it up just using |
Nice suggestion. I ended up with something like:
|
formRef.current.formElement.reportValidity() This works great. Nice. Thanks for the suggestion. I was strugling like anything to find a way for this. |
How did you attach a ref to the actual ?I've implemented this library at work and we created a custom component which uses the useTheme utility for generating our React-JSONSchema-Form stuff, though I would like to access the form props outside the Form component ie in an on the same page. Basically I want to save data before we do an action e.g. Expanding/Closing all Accordions on the page. What happens currently is the form data is reset, but if I could get access to the current form data I could save the data before hand :) Any guidance would be great 😉 |
When I called |
Prerequisites
Description
I want to call the form validation function elsewhere (outside of clicking the submit button). Is there a way to do it?
My use case involves creating a drop down menu, where each selection displays a different json-schema-form. There's a submit button, but it's to submit a collection of form data. I want to be able to validate each form when the user switches a selection.
Expected behavior
A form validation hook is available for me to invoke outside of calling onSubmit.
Version
"react-jsonschema-form": "^1.0.2"
The text was updated successfully, but these errors were encountered: