0

I have a function that will check if my textField has any issues

fun hasError(textFieldLabel: String):Boolean{}

I want this function to run only when the user has finished entering text. When the user exists the text field, we assume they're finished and I run hasError() on that field

Here is what didn't work for me

val focusManager = LocalFocusManager.current focusManager.moveFocus(FocusDirection.Next)

FocusDirection.Next Only moves the focus when you decide its appropriate, but doesn't listen when the user decides to move from one field to another

Modifier.onFocusChanged{} This doesn't work because it triggers when the page loads, and calls hasError(), leaving all my textFields in an error state

I want to trigger hasError when the user exits a text field

gmate
  • 39
  • 5

2 Answers2

1

You can validate TextField on exit by using onFocusChanged event of Modifier

modifier = Modifier
        .onFocusChanged {focusState ->
            if (!focusState.isFocused)
                //Perform validation here
        }
Murat Uygar
  • 529
  • 6
  • 10
0

You can simple listene for edit text change value. onValueChange is called when the text is changed and the text is finished,

  • On value changed is not listening for when the user exits the field. You're missing the point entirely. I'm not looking to keep the error state as the user is typing – gmate Jan 20 '23 at 21:40