I'm refactoring a large legacy web application that has some complicated forms.
These forms use javascript to display or hide fields on the basis of other entered fields. E.g ticking a checkbox for "second address" reveals a set of other fields to enter.
The current validation for this consists of lots of deeply nested if statements. Similar to:
if(checkboxTicked) {
haveMandatoryAddressFieldsBeenEntered();
if(addressHasbeenFilledIn) {
validateAddress()
}
}
Now imagine this example with much deeper nesting!
My question is - is there a nice pattern or best practice that I can refactor this with?
For reference I'm using Spring MVC - but I guess this would apply across multiple technologies.
When using MVC, I put my validation in my model. The model and controller don't know about how the form in the view looks like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With