jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

`onValidate` callback or similar for React

Open olliejm opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.

I have an issue where I want to change the validation mode of a form to ValidateAndShow only on submission, otherwise NoValidation. This causes a problem where, when I submit and change the mode, it's not possible to know when the validation has run if the form is valid, as onChange will not fire because neither data or errors have changed.

Describe the solution you'd like

It should be possible to react specifically to the event that validation just occurred, via an extra callback such as onValidate: (errors: ErrorObject[]) => void, which fires every time Ajv validates, regardless of the result.

Describe alternatives you've considered

I have been considering how it might be possible to pass a custom Ajv instance to the form and keep a reference of that instance on which to validate, but I'm not entirely sure it will work and at least would not be as clean as a simple event callback.

Framework

React

RendererSet

Vanilla

Additional context

We are using next versions of @jsonforms/core, @jsonforms/react, @jsonforms/vanilla-renderers

olliejm avatar Aug 17 '22 10:08 olliejm

Hi @olliejm, this is an interesting use case. Would it be sufficient for you if a regular onChange event would be emitted? Instead of only on data and errors changes we could also emit it for any validationMode change too.

sdirix avatar Aug 17 '22 12:08 sdirix

As a workaround you could also take over the whole validation yourself, i.e. always use noValidation. Once you want to show errors you can validate the data yourself in your AJV instance and then hand over the determined errors to JSON Forms via the additionalErrors prop. These errors are then shown even when noValidation is handed over.

By managing AJV yourself you know whether there are errors and can act accordingly.

sdirix avatar Aug 17 '22 12:08 sdirix

I've implemented a working solution for now where I handle the whole validation myself, which works fine.

The onChange firing when the form validates would offer the same functionality, the main reason I suggested onValidate was to avoid confusion caused by onChange firing without anything actually changing

Handling validation myself is probably a manageable solution for my case, but I'm not really sure one way or the other whether the library should provide a way to know that validation has occurred without having to create or pass a custom Ajv instance.

I guess possibly if you need such behaviour maybe it's likely that you're going to need to take control of Ajv yourself anyway

olliejm avatar Aug 17 '22 15:08 olliejm

As there was no further discussion I'll close this issue for now. Feel free to reopen if you disagree.

sdirix avatar Nov 08 '22 20:11 sdirix