andyhu92 / react-bootstrap4-form-validation

Simple React Components for form validation. Based on HTML5 Constraint validation API and Bootstrap4 style.
https://andyhu92.github.io/react-bootstrap4-form-validation/
MIT License
15 stars 4 forks source link

Validation condition on checkbox #6

Closed krajcikondra closed 5 years ago

krajcikondra commented 5 years ago

Hi,

I have e-shop with 3 steps order form. There is a form with delivery address, billing address and checkbox (Delivery address is same as billing address) in 2 step. When user dont checked checkbox then delivery address is required. When user checked checkbox then delivery address is not required.

So I need to add conditions to inputs in delivery address for checkbox checked.

Is there some way to do it?

andyhu92 commented 5 years ago

Thanks for using my library! One simple solution is when the user checks the checkbox you just copy the text from billing address input to delivery address input so the validation will be passed.

For conditional validation, you can pass a function to validator props of your delivery address input and return true or false based on your checkbox and delivery address state. I wrote an example for you here, and you can also find the example in my doc. @krajcikondra