formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Unable to override validation logic for custom component

Open aarpyy opened this issue 1 year ago • 7 comments

I have been unable to find in documentation or through looking at source code how a custom component can override validation logic. I'm opening this as a bug because Issue 4129 on this same repo opened this same issue as a bug with no response. Issue 369 was opened on the react repo and Issue 594 was opened on the angular repo, all for the same exact issue and no response.

I would greatly appreciate anyone's time and effort pointing me in the right direction, as I am largely unable to create the components I want without custom validation (the level of which I cannot do in the custom field in the schema).

aarpyy avatar Apr 21 '24 16:04 aarpyy

@aarpyy - Did you find any solution for this?

dinbtechit avatar Jul 10 '24 18:07 dinbtechit

It would be great to have custom validation for all components, not only custom components. the backend may have restrictions on the api key or number of keys, ... it should be easy to implement something like:

builder( div, {customValidation: (comp) => {return "some message if not valid, else null"}})

for custom components, one can easily check if comp is the custom component.

rzorzorzo avatar Sep 04 '24 09:09 rzorzorzo

Yeah so we've moved in our development branch (so master or any 5.x branch release candidate) to a "rules" based validation system (see @formio/core) from the old class-based one with lots of inheritance. This has a lot of upside when it comes to performance and readability, but a real downside is that it becomes hard to introduce your own bespoke validations. We're working on improving the API to include this for the upcoming releases (I believe we're targeting the very next patch after 5.0.0), but for now your best bet is custom validation in the builder for client-side validation. If you're still running the latest stable 4.x renderer, you should be able to override validation logic within the class itself by using the Validator class and attached validation functions.

brendanbond avatar Nov 19 '24 21:11 brendanbond

Thanks for the update @brendanbond! I think the Validator class is what I'm looking for documentation on but I haven't been able to find it, is there any chance you could help direct me? If not I will spend some time looking at the source for the Validator class to see how I can use it.

aarpyy avatar Nov 19 '24 21:11 aarpyy

It looks like there is no longer a Validator class in the @formio/js repository as the logic for validation has been moved to @formio/core

ZenMasterJacob20011 avatar Nov 20 '24 15:11 ZenMasterJacob20011

There's not really documentation on it, you can see that it has a collection of validators and each component adds or removes those validators. You might find some good example validation here as well in their custom components: https://github.com/SFDigitalServices/formio-sfds

brendanbond avatar Nov 20 '24 15:11 brendanbond

What tag / branch of formio.js are you using?

ZenMasterJacob20011 avatar Nov 20 '24 15:11 ZenMasterJacob20011