Closed frabanca closed 2 years ago
You can add a customValidation hook into the form that will allow you to send the data to a custom server url for validation first. You can then return a string/error object/array of errors that will then be displayed on the form. See:
https://github.com/formio/formio.js/blob/master/src/Webform.js#L1329-L1344
Hi @randallknutson , thank you very much for your reply!
I'm new to formio.js and I apologise for my question by... how can I do that? Can you make an example of a customValidation hook?
In the example I provided above, where (and how) should I add that hook?
Hi @randallknutson , I just found the path.
For everyone else reading this, I solved adding the following code to the options
param (the third one used to create the form instance)
// ...set var containerId and var formData
const formSettings = {
readOnly: false,
noAlerts: false,
hooks: {
customValidation: (submission, next) => {
// Call back-end APIs and handle response
}
}
};
// Init form
this.form = await Formio.createForm(
document.getElementById(containerId),
formData,
formSettings
);
Thanks for your help :)
Starting at what version is this hook "customValidation" available? And is it available to Wizard-based forms?
this hook has been around for a while and should also be available for any version released within the past year in 4.x branch. And yes it also applies to wizards.
Has anyone gotten this working with @formio/react? See sandbox: https://codesandbox.io/s/practical-chaum-rwgxqj?file=/src/index.js
I am adding this here as this issue appears on Google when searching for how to do this.
It appears to be an undocumented 'feature', but if you return a response in the following format then the formio.js framework displays the errors natively without writing a custom callback:
{
"name": "ValidationError",
"details": [
{
"message": "My custom message about this field",
"type": "custom",
"path": ["input1"],
"level": "error"
},
{
"message": "My custom message about another field",
"type": "custom",
"path": ["input2"],
"level": "error"
}
]
}
details
is an array with an object per input that is invalid. path
must be an array for some reason, but it takes the key of the input, and this is what puts the error in the right place in the form. Using this you can display a custom message for as many inputs as needed.
I am adding this here as this issue appears on Google when searching for how to do this.
It appears to be an undocumented 'feature', but if you return a response in the following format then the formio.js framework displays the errors natively without writing a custom callback:
{ "name": "ValidationError", "details": [ { "message": "My custom message about this field", "type": "custom", "path": ["input1"], "level": "error" }, { "message": "My custom message about another field", "type": "custom", "path": ["input2"], "level": "error" } ] }
details
is an array with an object per input that is invalid.path
must be an array for some reason, but it takes the key of the input, and this is what puts the error in the right place in the form. Using this you can display a custom message for as many inputs as needed.
Came here in 2024, really helped, thank you. Just want to add a small note: If you need to show errors programmatically (whenever/wherever you want), you can just invoke:
formioInstance.showErrors([
{
"message": "My custom message about this field",
"type": "custom",
"path": ["input1"],
"level": "error"
},
{
"message": "My custom message about another field",
"type": "custom",
"path": ["input2"],
"level": "error"
}
]
, true);
where formioInstance is the result of createForm:
const formioInstance = await Formio.createForm(<yourElement>, <yourSchema>, <yourOptions>);
Hi! I've just discovered this awesome framework and I'm trying to integrate it in a test environment in localhost. Everything looks fine to me but I don't undestand how to handle server-side errors. Let's suppose user submit the form but the server found a duplicate email address. My server will respond with a response body like this:
So my question is: How can I programmatically set custom error messages to each field of the form starting from fields' key?
EXAMPLE FIDDLE
Here's my code: HTML:
Javascript: