vuelidate: Is there a way to programmatically force an error?
Hi,
First, great library, love it. Also great idea to provide a base fiddle, that’s awesome 👍
I’m trying to do something which is either not possible, or I’m not aware of how to do it.
I’m hoping you can either guide me or confirm if it’s not possible.
Basically I would need to be able to programmatically set some validation errors based on some reply from the server.
It’s not the documented case of an async validation of a single field, I’m submitting the whole form data and need to highlight some of the field errors based on the server logic applied.
For example, image a credit card processing. If the credit card payment fails, i’d like to highlight the fields with particular validation error status (it’s not precisely my case but similar).
The problem I’m running into is that the error status booleans are apparently just getters, so if I try to force them, I get told off.
See this jsfiddle based on the sample you provided (again, great idea!)
https://jsfiddle.net/doginthehat/qc343jrd/
The result is: "Error: TypeError: Cannot set property required of #<Object> which has only a getter"
Is there a way to do this?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 19 (4 by maintainers)
I would just like to add/endorse that formal support for this action of accepting error messages from the server would be super awesome. When doing an AJAX form submit, there’s always the chance of a server-side validation test failure, even if they are all intended to be checked first client-side a programming error might mean a server-side check catches it. In particular, doing form submissions to a public JSON API interface which good-practice design requires it performs its own validation checks before committing changes. The main reasons why this concerns this validation framework are:
The method above herein is a good patch solution but probably not quite robust enough. I don’t just mean to be Mr. Negative Nelly… I am going to poke at solutions for this, and if I hit on something which might work well in a PR I will share, but beforehand just want to share my thoughts in case there are different opinions about this.
@Frizi
Yes! That’s the bit I was missing! 😄
And reading back to your original suggestion, you did mention it… my mistake.
Working fiddle for anyone coming across this thread
https://jsfiddle.net/doginthehat/LoLbfsoe/
Help much appreciated
I iterated on @doginthehat example and added server errors for two fields separately.
https://jsfiddle.net/duleorlovic/0tdpu32a
Based on error response from server we show error on specific fields. Server errors are checked in
.catchresponse for validationPromise and we clear validationPromise on update for those fields which can produce server errors.I finally ended with a
errorsobject in my data populated by my promise error callback. As my API return an error json object containing fields errors, it allows me to have custom validator which will check in thiserrorsobject if the field exist to return a true or false. And then, it’s easy to display the message associated in thev-ifthat display error messages.You should add validationPromise to data field, so it can be observable. Then the validator will kick in when you assign it and rerun when the promise state changes.