TextFieldData

data class TextFieldData(val value: String, val errorMessage: String? = null)(source)

Represents the state of a text input field with optional validation error.

This data class is designed to work within the UiState wrapper pattern, providing a clean way to manage text field state and validation errors. It's commonly used in screen data classes to represent form inputs.

Illustrative screen data (not an existing class)

data class LoginScreenData(
val email: TextFieldData = TextFieldData(""),
val password: TextFieldData = TextFieldData("")
)

Illustrative ViewModel excerpt (imports and repository omitted)

@HiltViewModel
class LoginViewModel @Inject constructor(
private val authRepository: AuthRepository
) : ViewModel() {
private val _uiState = MutableStateFlow(UiState(LoginScreenData()))
val uiState = _uiState.asStateFlow()

fun updateEmail(email: String) {
_uiState.updateState {
copy(email = TextFieldData(email, validateEmail(email)))
}
}

private fun validateEmail(email: String): String? {
return when {
email.isBlank() -> "Email cannot be empty"
!email.isEmailValid() -> "Invalid email format"
else -> null
}
}
}

Illustrative composable excerpt (imports omitted)

@Composable
fun LoginScreen(
screenData: LoginScreenData,
onEmailChange: (String) -> Unit
) {
JetpackTextField(
value = screenData.email.value,
onValueChange = onEmailChange,
label = { Text("Email") },
errorMessage = screenData.email.errorMessage,
)
}

UiText is available for localized UI text, but this class currently stores String? errors. Changing that type requires updating the form state and rendering calls together; the form guide explains the current validation and localization boundaries.

See also

Constructors

Link copied to clipboard
constructor(value: String, errorMessage: String? = null)

Properties

Link copied to clipboard

Optional validation error message. Null means no stored error, not proof that validation ran or that the backend accepts the value.

Link copied to clipboard

The current text content of the field