stormy-gold
stormy-gold2y ago

displaying field level validation errors from the backend

Hi, My backend returns validation structured validation errors on post or put, with field level error information for example:
{
"modelState": {
"name": "validate error"
},
"message": "Name: validate error"
}
{
"modelState": {
"name": "validate error"
},
"message": "Name: validate error"
}
How can I get the error for the name field to display next to the name field in refine. Is there a specific exception error structure I need to use?
21 Replies
flat-fuchsia
flat-fuchsia2y ago
stormy-gold
stormy-gold2y ago
Thanks for getting back to me, I've read the article and I can't find in it what I want looking for. I was hoping for a structure of an exception I as a dataProvider can throw in the create or update that'll be handled by refine to display the error correctly next to the field.
flat-fuchsia
flat-fuchsia2y ago
@noam.honig Im so sorry. I sent you wrong link 😦 https://refine.dev/docs/api-reference/core/providers/data-provider/#error-format this is the correct one
stormy-gold
stormy-gold2y ago
Thanks for that. In that error, how do I specify the exact error text I want to display next to each field? Something like:
{
message:"something is wrong",
statusCode:500,
errors:{
name:"Too Short",
age:"Too Young"
}
}
{
message:"something is wrong",
statusCode:500,
errors:{
name:"Too Short",
age:"Too Young"
}
}
flat-fuchsia
flat-fuchsia2y ago
Which form library u are using ?
stormy-gold
stormy-gold2y ago
I'm using the default one used in the tutorial
flat-fuchsia
flat-fuchsia2y ago
which tutorial ?
flat-fuchsia
flat-fuchsia2y ago
you can write custom form validation and error message like this https://refine.dev/docs/advanced-tutorials/forms/custom-form-validation/
stormy-gold
stormy-gold2y ago
I see - I was hoping for something built in refine that standardizes that as well
flat-fuchsia
flat-fuchsia2y ago
🙏 we will release refine 4 in 1 or 2 month. we will definitely consider this Hi @noam.honig, We discussed in the core team. We think your point is really valuable. Thank you 🙏 Do you wanna open feature request on Github ? If you open, we try to speed things up.
stormy-gold
stormy-gold2y ago
Sounds good
Omer
Omer2y ago
Remult looks great, congrats 🙂 Are you creating a dataprovider?
stormy-gold
stormy-gold2y ago
Hi @Omer, yes I'm creating a data provider, it seems fairly simple and powerful @Omer Since you mentioned remult, I would like to ask a follow-up question if that's ok. In remult I have a lot of metadata around the entities (field types, captions, validations etc...) This opens up the opportunity to create a powerful inferer that'll be based on entity metadata rather than JSON results. Is there a simple api for me to use to provide metadata to the refine inferer or do I need to create multiple inferers of my own?
Omer
Omer2y ago
@aliemirs can you help?
optimistic-gold
optimistic-gold2y ago
Hey @noam.honig, we have a createInferencer function in inferencer package that we use to create all of the inferencer components for our UI integrations. We're simply testing the response data fields by set of functions and send them to the renderer function which maps them and generates the code you see and able to copy. If you want to give it a try, you can also pass fieldTransformers array and customElements array in types FieldTransformer[] and FieldInferencer[] so you can provide custom types of fields to renderer and in that function you can generate the code for those types. You can try to experiment with it by using the codebase and by using the createInferencer function 🚀 Hope this helps 🙏
stormy-gold
stormy-gold2y ago
Can you provide some example usage of the createInferencer function - something where I provide to it the field names, captions and types and it'll render accordingly? I'm looking for a starter point I've played with the fieldTransformer property of the MUIInferer - but I was looking for a way to tell the Inferer which fields I have. I know the fields and their types and I don't want it to be inferred by the json result - I want it to also work when there are no rows at all @aliemirs (sorry, I forgot to mention you in my previous messages that were intended for you )
optimistic-gold
optimistic-gold2y ago
Got it @noam.honig, currently there's no way for it but I think you got the idea about how the inferencer works. We can modify the createInferencer to accept fields array and use it as the inffered fields. And for your case, you can then create a wrapper around it and determine the fields by your schema 🤔 Do you think this will work for you?
stormy-gold
stormy-gold2y ago
I think so, and I also think it could be useful for other inferers that you may want to create - infer from open api, infer from graphql etc...
optimistic-gold
optimistic-gold2y ago
Hey @noam.honig can you open a feature request issue on GitHub? We'll add this to our to-do and hopefully do a release soon 🙏
stormy-gold
stormy-gold2y ago
@aliemirs so far I've been only playing with refine - I don't want to open an issue that'll require your resources, before I have an actual use case