Form and "dynamic optional" fields

1 Answer 136 Views
Form Input
SysCo
Top achievements
Rank 1
Iron
Iron
SysCo asked on 07 Dec 2023, 03:16 PM

Hello,

I'm building a form where a field may become optional depending on a check box. I've set some validator on the field, but if I check the checkbox, the validators remain active on the field. Is there a way to dynamically attribute validators to a field ?

I try to disable the field, but even with the field beeing disabled, the validators are still working.

SysCo
Top achievements
Rank 1
Iron
Iron
commented on 12 Dec 2023, 10:23 AM

Moreover, disabled field should not be submitted and validated with the form. How to avoid that ?

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 12 Dec 2023, 12:05 PM

Hello,

For the dynamic validation you can use the approach demonstrated in the following example:

You will noticed that once the checkbox (or your custom condition) is changed to make the field optional, we need to trigger the validation once more using the onChange method.

As for disabling the input, due to the way that the Form is implemented, once the value is added within the "values" object, it will be submitted. With that in mind, once you disable the field dynamically, you can delete the value from the "values":

delete this.kendoForm.values["email"];

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources

SysCo
Top achievements
Rank 1
Iron
Iron
commented on 12 Dec 2023, 02:58 PM

Thanks, it definitely helps.
SysCo
Top achievements
Rank 1
Iron
Iron
commented on 12 Dec 2023, 03:23 PM

Almost helps :)

I just have an issue as my validators are in a separate js file. A bit like https://www.telerik.com/kendo-vue-ui/components/form/custom-components/#toc-setting-custom-behavior

I'm trying to pass the value of the checkbox to the validator, but can't find a way to make it work to pass the actual value of the field. I end up with Uncaught (in promise) TypeError: validator2 is not a function

Do you have a hint on how to resolve this ?

Konstantin Dikov
Telerik team
commented on 13 Dec 2023, 08:36 AM

Hi,

You will have to modify the logic and the validator in a way that you can pass the additional value (of the checkbox) to the validator.

I have modified the example, so it can use an external validator:

 

Tags
Form Input
Asked by
SysCo
Top achievements
Rank 1
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Share this question
or