I have been searching in the documentation and all over Google all evening to find out how I would render form errors for individual form fields in Leaf - something like this
It was my plan (unless there is a better way) but in the catch I am only able to get a list of error strings but not which field fails the validations.
I noticed that all error strings would start with the name of the failing field - e.g. "username is empty".
Would this be an okay way of doing it (only going to do something like this in this route)?
struct SignUpFormContext: Encodable {
var username: String?
var email: String?
var usernameError: String?
var emailError: String?
var passwordError: String?
}
func signupPostHandler(req: Request) async throws -> Response {
do {
try User.Create.validate(content: req)
} catch {
let data = try req.content.decode(User.Create.self)
var context = SignUpFormContext()
context.username = data.username
context.email = data.email
if let errors = error as? ValidationsError {
errors.failures.forEach { result in
if result.isFailure {
if let field = result.failureDescription?.components(separatedBy: " ").first {
switch field {
case "username":
context.usernameError = result.failureDescription
break
case "email":
context.emailError = result.failureDescription
break
case "password":
context.passwordError = result.failureDescription
break
default:
break
}
}
}
}
}
return try await req.view.render("auth/signup", context).encodeResponse(for: req)
}
...
}