Need help with form validation in Android and Anko


#1

So I have this function that checks if EditText fields are empty:

    private fun isFullForm(): Boolean {
        return when {
            company.text.isEmpty() or username.text.isEmpty() or password.text.isEmpty()
            -> return false
            else -> return true
        }
    }

And I this button block that uses that function to make it clickable:

   button("Login") {
       textSize = 26f
       val fields = listOf<EditText>(company, username, password)
       fields.forEach { it.textChangedListener { isClickable = isFullForm() } }
       onClick {
           //handleLogin(ctx)
           Snackbar.make(this@verticalLayout, "Login Handled", 
                   Snackbar.LENGTH_SHORT).show()
        }
    }

But the function doesn’t seem to be updating the isClickable variable. Any idea what I’m doing wrong?


#2

So here is what I have been able to get it nearly working for anyone trying to implement something similar:

                login = button("Login") {
                    textSize = 26f
                    onClick {
                        //handleLogin(ctx)
                        Snackbar.make(this@verticalLayout, "Login Handled",
                                Snackbar.LENGTH_SHORT).show()
                    }
                }

                val fields = listOf<EditText>(company, username, password)
                fields.forEach {
                    it.textChangedListener {
                        onTextChanged { _, _, _, _ -> login.isClickable = isFullForm() }
                        beforeTextChanged { _, _, _, _ -> login.isClickable = isFullForm() }
                        afterTextChanged { _ -> login.isClickable = isFullForm() }
                    }
                }

And for the isFullForm function:

    private fun isFullForm(): Boolean {
        return when {
            company.editableText.isNullOrBlank() or
            username.editableText.isNullOrBlank() or
            password.editableText.isNullOrBlank() -> false
            else -> true
        }
    }

The only issue I am still having with this implementation is when the app initially launches, the button is clickable. Any help is appreciated.