Validating Input
Check input and show helpful errors.
Why Validate Input
Users make mistakes: an empty name, a malformed email, a too-short password. Good forms catch these early and explain clearly what to fix.
In this lesson you'll add validation to Compose forms, show inline errors, and disable the submit button until everything is valid.
A Validation Rule is Just a Function
Keep validation logic as plain Kotlin functions that take a value and return whether it's valid (or an error message). This makes rules reusable and easy to test.
Here is a simple email check.
fun emailError(email: String): String? = when {
email.isBlank() -> "Email is required"
!email.contains("@") -> "Enter a valid email"
else -> null
}