1

I am new to Android development, I'm trying to make form validation using RxKotlin and RxBinding.

I need a guidance how to make form validation with more than 9 fields? Actually I can combine the result using Observable.combinelatest.

This is the code I've been trying:

Observable.combineLatest(profileObserver, shopName, shopAddress, ownerName, idCard, ownerHp, ownerEmail, pin, confirmPin,
                Function9<CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence,
                        Boolean> {profile, name, address, owner, card, hpNumber, email, currentPin, confirmationPin ->
                    return@Function9 isShopNameValid(name.toString()) && isShopAddressValid(address.toString())
                            && isOwnerNameValid(owner.toString())
                            && isIdCardValid(card.toString())
                            && isOwnerHpValid(hpNumber.toString())
                            && isOwnerEmail(email.toString())
                            && isPinValid(currentPin.toString())
                            && isConfirmPinValid(confirmationPin.toString())
                }).subscribe {
                registrationProcess.isEnabled = it
            }

I still have 3 more fields to be validated.

musooff
  • 6,412
  • 3
  • 36
  • 65
  • (1) The code is incomplete. Can you complete your sample and format it? (2) What is significant about a form with more than 9 fields? (3) What are the 3 fields you need to have validated, what have you tried, and what isn't working? – Jake Reece Jul 24 '19 at 18:14
  • Actually I'm trying to make a registration form with 13 edittext and all the fields is mandatory. The button to proceed the registration is disable until the validation is valid. The validation on each edittext is different from each other. I think I already succeed with 9 fields. But for more than 9 fields I'm still struggle with. – Taslim Hartmann Jul 25 '19 at 06:04

1 Answers1

0

there is a Observable.combineLatest method that takes an Iterable of observables for this case. Make a list of all of your form field observables and use that version of combineLatest.

val fieldsList = listOf(shopName) //etc
Observable.combineLatest(fieldsList){arrayOfLatest ->
  //validate fields 
  isValidShopName(arrayOfLatest[0])// etc
}

I would also consider having each observable validate itself in this case. Example

shopName.map{
  isValidShopName(it)
}
//and so on for each field

//Now combine into list as in above example
Observable.combineLatest(validatedFieldsList){arrayOfResults ->
   arrayOfResults.any{!it} //If true there is an invalid field
}
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91