material-components/material-components-android-codelabs

MDC-101 Kotlin - step 5 - error message in password field not working

davimuri opened this issue · 1 comments

I had to modifiy the code to make it work:

       // Set an error if the password is less than 8 characters.
        view.next_button.setOnClickListener({
            if (!isPasswordValid(view.password_edit_text.text!!)) {
                view.password_text_input.error = getString(R.string.shr_error_password)
            } else {
                // Clear the error.
                view.password_text_input.error = null
                // Navigate to the next Fragment.
                (activity as NavigationHost).navigateTo(ProductGridFragment(), false)
            }
        })

        // Clear the error once more than 8 characters are typed.
        view.password_edit_text.setOnKeyListener({ et, _, _ ->
            if (isPasswordValid((et as EditText).text!!)) {
                // Clear the error.
                (et.parent as TextInputLayout).error = null
            }
            false
        })

Revisited code:

        // Set an error if the password is less than 8 characters.
        view.next_button.setOnClickListener({
            if (!isPasswordValid(view.password_edit_text.text!!)) {
                view.password_text_input.error = getString(R.string.shr_error_password)
            } else {
                // Clear the error.
                view.password_text_input.error = null
                // Navigate to the next Fragment.
                (activity as NavigationHost).navigateTo(ProductGridFragment(), false)
            }
        })

        // Clear the error once more than 8 characters are typed.
        view.password_edit_text.setOnKeyListener({ _, _, _ ->
            if (isPasswordValid(view.password_edit_text.text!!)) {
                // Clear the error.
                view.password_text_input.error = null
            }
            false
        })