Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forms screen to conditionally display error text and validate fields #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,29 @@ fun FormsScreen(modifier: Modifier = Modifier) {
)

var textFieldValue by remember { mutableStateOf("") }
var emailError by remember { mutableStateOf(false) }
val badErrorText = "Le champ email est en erreur"
val betterErrorText = "Le champ email est obligatoire. Format attendu : [email protected]"
CustomTextField(
value = textFieldValue,
onValueChange = { textFieldValue = it },
label = { Text("Email") },
supportingText = { Text(badErrorText) },
isError = true,
supportingText = { if (emailError) Text(betterErrorText) else null },
isError = emailError,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
)

var textFieldPhoneValue by remember { mutableStateOf("") }
var phoneError by remember { mutableStateOf(false) }

CustomTextField(
value = textFieldPhoneValue,
onValueChange = { textFieldPhoneValue = it },
label = { Text("Telephone") },
supportingText = { Text("Le numéro de téléphone doit contenir exactement 10 chiffres") },
supportingText = { if (phoneError) Text("Le numéro de téléphone doit contenir exactement 10 chiffres") else null },
isError = phoneError,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
Expand All @@ -68,7 +71,9 @@ fun FormsScreen(modifier: Modifier = Modifier) {
val context = LocalContext.current
val sentText = "Formulaire enregistré avec succès"
Button(
onClick = { sent = true },
onClick = {
validateForm(textFieldValue, textFieldPhoneValue)
},
modifier = Modifier.padding(16.dp),
enabled = !sent
) {
Expand Down Expand Up @@ -116,6 +121,18 @@ fun CustomTextField(
}
}

fun validateForm(email: String, phone: String) {
emailError = email.isEmpty() || !email.contains("@")
phoneError = phone.length != 10
if (emailError) {
// Give focus to the email field
} else if (phoneError) {
// Give focus to the phone field
} else {
sent = true
}
}

@Preview
@Composable
private fun FormsScreenPreview() = IpedisAndroidAccessibilityCourseTheme {
Expand Down