-
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
Cannot check for existing errors on field in CustomValidate #4284
Comments
@TheOneTheOnlyJJ Could you create a CodeSandbox reproduction that demonstrates that |
Sure, here you go: https://codesandbox.io/p/sandbox/rjsf-field-errors-3ndqph |
@nickgros |
@TheOneTheOnlyJJ sorry for the delay. I took a closer look and discovered that we don't pass existing errors to You might be able to use the transformErrors function, with something like this: import { ErrorTransformer } from "@rjsf/utils";
const transformErrors: ErrorTransformer = (errors) => {
// Skip if no form data or errors
let hasErrorInUserName = false;
errors.forEach((e) => {
if (e.property === ".username") {
hasErrorInUserName = true;
}
});
if (!hasErrorInUserName) {
// Synchronous API call...
errors.push({
name: "Username taken",
stack: ".username",
message: "Username exists in system",
});
}
return errors;
}; |
Prerequisites
What theme are you using?
core
Version
5.20.0
Current Behavior
I have a username field in my form that I want to invalidate if it is not unique. I check its uniqueness with an API call, which always runs in my CustomValidate function. However, it would be more efficient only to run it if there are no other prior errors on that field (I have a pattern in the JSON schema that disallows certain characters). I am trying to do a simple if based on the
errors.username.__errors
variable, but no matter in which configuration I write the condition, I can only get it to always be true or never be true, regardless of the presence of other errors.Expected Behavior
I expect a check such as
if (!errors.username.__errors || errors.username.__errors.length === 0)
to be true if there are no errors associated to theusername
field, and false if there is at least one.Steps To Reproduce
No response
Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: