-
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
Validate onBlur? #617
Comments
I have the same question. |
@eddyzhang1986 Its not possible to do so. I recommend you to create your own implementation of jsonschema generator, its not hard and you ll have all flexibility you need. |
A demo of OO solution for validate onBlur for the whole form. import Form from 'react-jsonschema-form';
class BlurForm extends Form {
constructor(props) {
super(props);
const superOnBlur = this.onBlur;
this.onBlur = (...args) => {
const { formData } = this.state;
const { errors, errorSchema } = this.validate(formData);
this.setState({ errors, errorSchema },()=>superOnBlur(...args));
}
}
} The rest is the same, just don't enable live validation. My experience was that the on blur is unreliable. The on blur event came after the on click which mean if users click submit in the middle of editing a field, it fnck up. |
@knilink thanks for the code snippet! It works like a charm. In my scenario I blocks form submission and force the users to fix their input before submit. Validating when blur event is fired would help users visualizing their progress in fixing inputs. I agree that on blur is unreliable. Therefore, I would like to validate both on submit and on blur, to strike a balance between user experience and reliability. |
@knilink thanks for the snippet! but it validates all fields (even the untouched ones) when blur event is triggered. Is it possible to perform onBlur validation per field? |
At present, react-jsonschema-form doesn't attempt to validate any user input at anything less than "the entire form" level. |
Why was this closed? This should be kept open and a prop should be added to the |
I was able to implement this using a combination of liveValidate on the form and a few higher order components to manually track blurred state and show errors only once a field has been blurred on my custom widgets. Here's what that could look like: import {
compose,
mapProps,
withStateHandlers,
} from "recompose";
const rawErrorsToErrorMessage = mapProps(
({ rawErrors, isBlurred, ...props }) => ({
...props,
// TODO: Also need to show all errors after first submit
errorMessage: isBlurred && rawErrors && (
<React.Fragment>
{rawErrors.map((error) => (
<div>{error}</div>
))}
</React.Fragment>
),
}),
);
const withBlurred = withStateHandlers(
{ isBlurred: false },
{
onBlur: () => () => ({
isBlurred: true,
}),
},
);
const transformWidgetProps = compose(
withBlurred,
rawErrorsToErrorMessage,
);
const widgets: any = {
text: transformWidgetProps(TextInput),
select: transformWidgetProps(OptionInput),
}; Hope this could be useful to others here. UPDATE: Never mind, I later ran into a brick wall trying to show errors after the first submit attempt by wrapping a state tracking HOC around the form. For some reason, when the form gets rerendered, inputs lose their focus. |
Create an example for selective field level onBlur validation. Hope it helps anyone facing this issue ! |
hope this help issue: 431 |
@KKS1 Thank you for putting together a proof of concept! I'm trying to get your example to work with material-ui and running into a problem. Since you're extending the Form, it requires: and when I try this:
I get an error How do I get onblur validation to work with Material UI? Any help would be greatly appreciated. |
Did it work with |
@epicfaace No, same error. |
@stvhanna Created an end-to-end Material UI based implementation, using function based approach. Of-course, we can further optimize it using one time run effect, which can then call referenced Form onBlur if needed. Hope it helps, cheers! |
This is stale but I thought I'd share my approach for anyone still looking. I expanded one of the previous implementations, here's a simplified version of my solution: I turned off the form validation and kept the errors in the state myself, passing them to the form through the |
Prerequisites
Description
Is it possible to validate a field on Blur event? I would like to do that for, for example, email input.
The text was updated successfully, but these errors were encountered: