How to display error messages for multiple TextField in jetpack compose. with only one field:
private var isError by mutableStateOf(false)
private fun validate(text: String){
isError = if(text.isEmpty()){
true
}else{
android.util.Patterns.EMAIL_ADDRESS.matcher(text).matches()
}
Log.i("Boolean",isError.toString())
}
TextField(value = email,placeholder = { Text(text = "E-mail")},
onValueChange = {
email=it
isError = false
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent
),
singleLine = true,
isError = isError,
keyboardActions = KeyboardActions { validate(email) },
modifier=Modifier.align(Alignment.CenterHorizontally),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
leadingIcon = { Icon(imageVector = Icons.Default.Email, contentDescription = null) })
I have a form with many TextField how do I validate one by one. for example if I have two fields with name and email. I thought about doing a loop with all the fields but I don't know if it's the best practice. Can someone help me
var nome by rememberSaveable{ mutableStateOf("")}
var email by rememberSaveable{ mutableStateOf("") }
TextField(value = nome,placeholder = { Text(text = "Nome")},
onValueChange = {
nome=it
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent
),
modifier=Modifier.align(Alignment.CenterHorizontally),
leadingIcon = { Icon(imageVector = Icons.Default.Person, contentDescription = null) })
Spacer(modifier = Modifier.padding(5.dp))
TextField(value = email,placeholder = { Text(text = "E-mail")},
onValueChange = {
email=it
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent
),
modifier=Modifier.align(Alignment.CenterHorizontally),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
leadingIcon = { Icon(imageVector = Icons.Default.Email, contentDescription = null) })
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = { verifyEmpty(strings=validate) },
colors = ButtonDefaults.buttonColors(
contentColor = colorResource(id = R.color.marron),
backgroundColor = colorResource (id = R.color.pastel_green)
),
) {
Text(text = stringResource(id = R.string.view_cad),
color= colorResource(id = R.color.marron))
}