Disabling and enabling items in forms
Form
fields may have additional requirements such as minimum text length or a combination of uppercase and lowercase characters. We may want to perform actions based on the user's input, such as disabling a Submit button until all requirements are met.In this recipe, we will create a sign-in view where the Submit button only gets enabled if the user enters some content in both the username and password fields.
Getting ready
Create a SwiftUI project called FormFieldDisable
.
How to do it…
We will create a login screen containing a username, password, and a Submit button. We will disable the Submit button by default and only enable it when the user enters some text in the username and password fields. The steps are given here:
- Create a new SwiftUI view file named
LoginView
:- Press Command () + N.
- Select SwiftUI View.
- Click Next.
- Enter LoginView in the Save as field.
- Click Create.
- Open the
LoginView.swift
file and add a@State
variable...