Keep SwiftUI Form Validation Useful With VoiceOver
A form turns two borders red when you press Save. Visually, the failure seems obvious. With VoiceOver, the button may simply finish speaking and leave you wondering whether anything happened.
The missing piece is an explanation of the failed submission and a useful place to continue. Let’s build a small project form that exposes both, without announcing every intermediate keystroke as an error.
Keep validation independent of presentation
Our form needs an owner’s name and a project title. The validator returns ordered issues with field identities. It does not move focus, post announcements, or erase input.
That separation matters because the same rules may serve a keyboard submission, a button, or a test. The first issue should follow the form’s reading order rather than the iteration order of a dictionary.
Here is the complete example. The state object is owned by this form; an existing app can put the same draft and submission state in its current form model.
import Combine
import SwiftUI
struct ProjectDraft {
var owner = ""
var title = ""
enum Field: Hashable { case owner, title }
struct Issue: Equatable {
let field: Field
let message: String
}
var issues: [Issue] {
var result: [Issue] = []
if owner.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
result.append(Issue(field: .owner, message: "Enter the project owner's name."))
}
if title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
result.append(Issue(field: .title, message: "Enter a project title."))
}
return result
}
}
@MainActor
final class ProjectFormState: ObservableObject {
@Published var draft = ProjectDraft()
@Published var attemptedSave = false
}
struct ProjectForm: View {
@StateObject private var state = ProjectFormState()
@FocusState private var editing: ProjectDraft.Field?
@AccessibilityFocusState(for: .voiceOver) private var summaryFocused: Bool
let save: (ProjectDraft) -> Void
private var visibleIssues: [ProjectDraft.Issue] {
state.attemptedSave ? state.draft.issues : []
}
private var summary: String {
guard !visibleIssues.isEmpty else {
return "Enter an owner and a title to create a project."
}
return "Project not saved. " + visibleIssues.map(\.message).joined(separator: " ")
}
var body: some View {
Form {
Section {
Text(summary)
.accessibilityFocused($summaryFocused)
}
Section("Project details") {
TextField("Owner", text: $state.draft.owner)
.focused($editing, equals: .owner)
.submitLabel(.next)
.onSubmit { editing = .title }
if let issue = visibleIssues.first(where: { $0.field == .owner }) {
Text(issue.message).foregroundStyle(.red)
}
TextField("Project title", text: $state.draft.title)
.focused($editing, equals: .title)
.submitLabel(.done)
.onSubmit(submit)
if let issue = visibleIssues.first(where: { $0.field == .title }) {
Text(issue.message).foregroundStyle(.red)
}
}
Button("Create project", action: submit)
}
}
private func submit() {
state.attemptedSave = true
guard state.draft.issues.isEmpty else {
editing = nil
summaryFocused = true
return
}
save(state.draft)
}
}
The rule intentionally checks only whether a required value is blank. Names do not need an ASCII-only regular expression, and a project title should not fail because it contains a different writing system. The save layer still owns whatever authorization and business rules the product actually requires.
Explain the failure in two places
The summary says the project was not saved and lists the corrections. Inline messages remain beside their fields so the user can revisit each problem without returning to the top.
Color reinforces those messages but does not carry the explanation by itself. An accessibility hint should not be the only place an essential error lives either. Apple’s hint documentation describes hints as brief explanations of an action’s result. A current validation failure belongs in content the user can discover again.
The Create button stays enabled while the form is incomplete. In this design, pressing it reveals why the operation cannot proceed. A disabled button with no explanation would remove that route to understanding the requirements. During a real asynchronous save, a separate in-progress state can prevent duplicate submissions while showing that work is underway.
Treat the two kinds of focus separately
AccessibilityFocusState and keyboard editing focus have different jobs. The sample ends editing after an invalid submission and requests VoiceOver focus on the persistent summary element. The summary exists before submission, so this request does not depend on inserting a new element and guessing how many milliseconds to wait for it.
The .voiceOver scope is intentional. It avoids claiming that a VoiceOver-specific focus policy is automatically appropriate for every assistive technology. Test other interaction methods separately.
The focus binding also reflects focus changes made by the user. Do not treat its Boolean as a command queue or repeatedly force it to true while someone is exploring the screen.
This example chooses a summary-first experience. For a long form, a summary with navigable links to invalid fields may be more useful. A short form might instead focus the first invalid field while exposing its associated error. Choose one coherent interaction and listen to the result before layering announcements over it.
Validate after an attempt without interrupting correction
Before the first submission, the form explains what is needed without showing an error for every untouched field. After an attempt, the visible issues are derived from the current draft. Correcting a field removes its error.
That recalculation does not request focus. Only an explicit invalid submission does. Otherwise, typing the final character in a valid name could pull the user away from the field they are editing.
The summary may change while the user corrects input. Do not assume a changing string will always be spoken at the right moment, or add an announcement for every change. The persistent inline messages and the next submission provide deliberate ways to inspect the current state.
For server-side validation, attach the response to the submitted draft revision. If the user edits the title while a request is running, a late “title already exists” response for the old title should not become the current field’s error.
Test the rules, then test the conversation
The code fixture checks blank strings, whitespace-only values, stable issue ordering, and non-ASCII names and titles. Those are useful checks, but they cannot prove that VoiceOver focus landed or that the explanation sounds natural.
On a device, submit the empty form, listen to the summary, navigate to each field, correct one value, and submit again. Verify the remaining message is accurate and the previous input survives. Repeat using the keyboard’s Next and Done actions. Try a large accessibility text size so error text has room to wrap.
September 2026 review: the full view type-checks for iOS 17 against the iOS 27 SDK with Swift 6.4. The standalone validation assertions passed on macOS. VoiceOver speech, keyboard transitions, and device focus behavior remain integration checks; compilation does not verify them.
A useful validation flow tells the user what happened, what needs attention, and how to get back to the work. The red border is only one optional part of that explanation.