var idErrorMessage: String? @State var isIDErrorActive = false @State var password = "" @State var passwordErrorMessage: String? @State var isPasswordErrorActive = false @State var favoriteFruit: Fruit? @State var favoriteFruitErrorMessage: String? @State var isFavoriteFruitErrorActive = false func register() -> RegisterRequest? { idErrorMessage = nil isIDErrorActive = false passwordErrorMessage = nil isPasswordErrorActive = false favoriteFruitErrorMessage = nil isFavoriteFruitErrorActive = false if id.isEmpty || id.count > 20 { idErrorMessage = "ID1~20จࣈҎͰೖྗ͍ͯͩ͘͠͞" isIDErrorActive = true } if password.isEmpty || password.count > 20 { passwordErrorMessage = "ύεϫʔυ1~20จࣈҎͰೖྗ͍ͯͩ͘͠͞" isPasswordErrorActive = true } if favoriteFruit == nil { favoriteFruitErrorMessage = "͖ͳϑϧʔπΛબ͍ͯͩ͘͠͞" isFavoriteFruitErrorActive = true } if isIDErrorActive || isPasswordErrorActive || isFavoriteFruitErrorActive { return nil } let request = RegisterRequest( id: id, password: password, favoriteFruit: favoriteFruit! ) return request } } struct RegisterView_ver1: View { var body: some View { ScrollView { VStack(spacing: 16) { MyTextField(name: "ID", text: $id) .errorStyled(isActive: isIDErrorActive, errorMessage: idErrorMessage) .onChange(of: id) { _ in isIDErrorActive = false } MyTextField(name: "PW", text: $password) .errorStyled(isActive: isPasswordErrorActive, errorMessage: passwordErrorMessage) .onChange(of: password) { _ in isPasswordErrorActive = false } FruitPicker(name: "͖ͳϑϧʔπ", selection: $favoriteFruit) .errorStyled(isActive: isFavoriteFruitErrorActive, errorMessage: favoriteFruitErrorMessage) .onChange(of: favoriteFruit) { _ in isFavoriteFruitErrorActive = false } Button("ొ") { register() } .buttonStyle(.myButtonStyle) } .padding() } } func register() { Task { await API.send(request) } } }