Kotlin: "when Statement" Skips Condition
I am trying to validate a form in android using Kotlin. My problem is, that my when statement does not work like it's supposed to work. It doesn't check for the other conditions wh
Solution 1:
when
works exactly the way it's supposed to work (emphasis mine):
when
matches its argument against all branches sequentially until some branch condition is satisfied.
So it executes at most one branch, the first matching one, and then it stops.
If you want to continue to match other branches, just use a series of unrelated if
statements instead:
// It is possible, that both btNumber and ddSalutation are false, but it only checks for btNumber// and then jumps outfunisFormValid(): Boolean {
var error = 0if (btNumber.value.isNullOrEmpty()) {
btNumberEM.value = emptyFieldError
error = 1
}
if (btNumber.value.toString().length < 7) {
btNumberEM.value = btNumberTooFewChar
error = 1
}
if (ddSalutation.value == ddSalutationTextPlaceHolder) {
ddSalutationEM.value = ddSalutationNotSelected
error = 1
}
if (etFirstName.value.isNullOrEmpty()) {
etFirstNameEM.value = emptyFieldError
error = 1
}
return error == 0
}
Post a Comment for "Kotlin: "when Statement" Skips Condition"