Forms and Input Handling in iOS 18 – #30DaysOfSwift

Forms are a core part of many apps for collecting data from users.


This content originally appeared on HackerNoon and was authored by Vaibhav

Day 24: Designing Forms and Handling User Input Validation 📋

\ Let's go to the basics today with by creating forms and handling user input in SwiftUI. Forms are a core part of many apps for collecting data from users.

Image description

Let’s jump in!


Code

SwiftUI provides the Form container, making it easy to create user-friendly input forms. \n

import SwiftUI

struct UserFormView: View {
    @State private var username = ""
    @State private var email = ""
    @State private var password = ""
    @State private var errorMessage = ""

    var body: some View {
        Form {
            // Username Input
            Section(header: Text("Username")) {
                TextField("Enter your username", text: $username)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }

            // Email Input
            Section(header: Text("Email")) {
                TextField("Enter your email", text: $email)
                    .autocapitalization(.none)
                    .keyboardType(.emailAddress)
                    .disableAutocorrection(true)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }

            // Password Input
            Section(header: Text("Password")) {
                SecureField("Enter your password", text: $password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }

            // Error Message
            if !errorMessage.isEmpty {
                Text(errorMessage)
                    .foregroundColor(.red)
            }

            // Submit Button
            Button(action: {
                validateForm()
            }) {
                Text("Submit")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            }
        }
        .navigationTitle("User Form")
    }

    // Step 3: Validate the form inputs
    func validateForm() {
        if username.isEmpty || email.isEmpty || password.isEmpty {
            errorMessage = "All fields are required!"
        } else if !email.contains("@") {
            errorMessage = "Invalid email address!"
        } else if password.count < 6 {
            errorMessage = "Password must be at least 6 characters!"
        } else {
            errorMessage = ""
            // Handle successful form submission (e.g., save data)
        }
    }
}

\ The full series is available on my profile and the components can also be found at shipios.app/components.

\ Happy Coding!


This content originally appeared on HackerNoon and was authored by Vaibhav


Print Share Comment Cite Upload Translate Updates
APA

Vaibhav | Sciencx (2024-11-01T16:40:54+00:00) Forms and Input Handling in iOS 18 – #30DaysOfSwift. Retrieved from https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/

MLA
" » Forms and Input Handling in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Friday November 1, 2024, https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/
HARVARD
Vaibhav | Sciencx Friday November 1, 2024 » Forms and Input Handling in iOS 18 – #30DaysOfSwift., viewed ,<https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/>
VANCOUVER
Vaibhav | Sciencx - » Forms and Input Handling in iOS 18 – #30DaysOfSwift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/
CHICAGO
" » Forms and Input Handling in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Accessed . https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/
IEEE
" » Forms and Input Handling in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx [Online]. Available: https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/. [Accessed: ]
rf:citation
» Forms and Input Handling in iOS 18 – #30DaysOfSwift | Vaibhav | Sciencx | https://www.scien.cx/2024/11/01/forms-and-input-handling-in-ios-18-30daysofswift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.