Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Last active April 9, 2024 00:28
Show Gist options
  • Save hmlongco/74a268dcfc9847eadc53d220c36b450c to your computer and use it in GitHub Desktop.
Save hmlongco/74a268dcfc9847eadc53d220c36b450c to your computer and use it in GitHub Desktop.
GTP4 Form
struct User {
var firstName: String
var lastName: String
var email: String
var phoneNumber: String
var street: String
var city: String
var state: String
var zipCode: String
}
class UserInfoViewModel: ObservableObject {
@Published var firstName: String = ""
@Published var lastName: String = ""
@Published var email: String = ""
@Published var phoneNumber: String = ""
@Published var street: String = ""
@Published var city: String = ""
@Published var state: String = ""
@Published var zipCode: String = ""
@Published var errorMessage: String = ""
var isFormValid: Bool {
!firstName.isEmpty && !lastName.isEmpty && !email.isEmpty && !phoneNumber.isEmpty &&
!street.isEmpty && !city.isEmpty && !state.isEmpty && !zipCode.isEmpty
}
func saveUserInfo() {
if isFormValid {
let user = User(firstName: firstName, lastName: lastName, email: email, phoneNumber: phoneNumber, street: street, city: city, state: state, zipCode: zipCode)
print("User Info Saved: \(user)")
errorMessage = ""
} else {
errorMessage = "Please fill in all fields."
}
}
}
struct UserInfoView: View {
@StateObject var viewModel = UserInfoViewModel()
var body: some View {
NavigationView {
Form {
if !viewModel.errorMessage.isEmpty {
Text(viewModel.errorMessage)
.foregroundColor(.red)
.font(.callout)
.padding()
.accessibilityIdentifier("errorMessage")
.accessibilityLabel("Error Message")
}
Section {
TextField("First Name", text: $viewModel.firstName)
.accessibilityIdentifier("firstNameField")
.accessibilityLabel("First Name Field")
TextField("Last Name", text: $viewModel.lastName)
.accessibilityIdentifier("lastNameField")
.accessibilityLabel("Last Name Field")
TextField("Email", text: $viewModel.email)
.keyboardType(.emailAddress)
.accessibilityIdentifier("emailField")
.accessibilityLabel("Email Field")
TextField("Phone Number", text: $viewModel.phoneNumber)
.keyboardType(.phonePad)
.accessibilityIdentifier("phoneNumberField")
.accessibilityLabel("Phone Number Field")
}
Section {
TextField("Street", text: $viewModel.street)
.accessibilityIdentifier("streetField")
.accessibilityLabel("Street Field")
TextField("City", text: $viewModel.city)
.accessibilityIdentifier("cityField")
.accessibilityLabel("City Field")
TextField("State", text: $viewModel.state)
.accessibilityIdentifier("stateField")
.accessibilityLabel("State Field")
TextField("Zip Code", text: $viewModel.zipCode)
.keyboardType(.numberPad)
.accessibilityIdentifier("zipCodeField")
.accessibilityLabel("Zip Code Field")
}
Button("Save") {
viewModel.saveUserInfo()
}
.disabled(!viewModel.isFormValid)
.accessibilityIdentifier("saveButton")
.accessibilityLabel("Save Button")
}
.navigationTitle("User Info")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment