'Placeholder not displaying after setting prompt in SecureField
The prompt inside my secure field is supposed to display when the user doesn't meet the password credentials and disappear when password credentials are met.
As of now, it is acting as a placeholder. I only included snippets of the code, as I figured the problem was within the lines of code below.
Snippet
@ObservedObject var userInformation = FormViewModel()
SecureField("Password", text: $userInformation.password,
prompt: Text(userInformation.passwordPrompt))
}
class FormViewModel: ObservableObject {
@Published var password = ""
func isPasswordValid() -> Bool {
let passwordTest = NSPredicate(format: "SELF MATCHES%@", "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$")
return passwordTest.evaluate(with: password)
}
var isSignUpComplete: Bool {
if !isPasswordValid() || !isEmailValid() || firstname == "" || lastname == "" {
return false
}
return true
}
var passwordPrompt: String {
if isPasswordValid(){
return ""
} else {
return "Must be between 8 and 15 letters, containing at least one capital and one number"
}
}
}
Solution 1:[1]
Silly question, sometimes you just get overwhelmed and overlook a simple solution. For this, I just added an or statement to the password validation to check if the password is an empty string as well. Hiding my prompt.
var passwordPrompt: String {
if isPasswordValid() || password == ""{
return ""
} else {
return "Must be atleast"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Swink |

