Search code examples
iosswiftvalidationviewsecurefield

Type '()' cannot conform to 'View' on SWIFT


I'm getting this error when I try to validate password field under a SecureTextField field.

any thoughts? I did similar with an TextField for Email validation and works just fine! my issue is with the password validation "If"

                HStack {
                
                    TextField("Email Address", text: $loginVM.credentials.email) { isEditing in
                        self.isEditing = isEditing
                        
                        errorMessage = ""
                        ////// == getting  email validation boolean after losing  the focus
                        if  isEditing  == false  {
                            if validateEmail(emailAddressString: loginVM.credentials.email) ==  true {
                                errorMessage = "YES, Email is Good"
                                print(errorMessage)
                                } else {
                                errorMessage = "*-Oops,Something is wrong with your email !!!"
                                print(errorMessage)

                            }
                        }
                       
                    }
                   
                    } .textFieldStyle(MyTextFieldStyle(focused: $isEditing))
                    
                        
                HStack {
                    
                    SecureTextField(text: $loginVM.credentials.password)

                            .padding(10)
                            .focused($isInFocus)
                            .background(
                            RoundedRectangle(cornerRadius: 10, style: .continuous)
                            .stroke(isInFocus ? Color.orange : Color.gray, lineWidth: 1))
                    
                               
                            }.disableAutocorrection(true)
                // Here is where I'm getting this Type '()' cannot conform to 'View'
                            if validatePassword(passwordString: loginVM.credentials.password) ==  true {
                                errorMessage = "YES, Password  OK"
                                print(errorMessage)
                                } else {
                                errorMessage = "*-Oops,Something is wrong with your Password !!!"
                                print(errorMessage)

                            }

Solution

  • you can wrap your logic inside onSubmit.

    the compiler expects some View inside the ViewBuilder

    SecureTextField(text: $name)
        .padding(10)
        .focused($isInFocus)
        .disableAutocorrection(true)
        .background(
            RoundedRectangle(
                cornerRadius: 10,
                style: .continuous
            )
            .stroke(isInFocus ? Color.orange : Color.gray, lineWidth: 1)
        )
        .onSubmit {
            if validatePassword(passwordString: loginVM.credentials.password) {
                errorMessage = "YES, Password  OK"
                print(errorMessage)
            } else {
                errorMessage = "*-Oops,Something is wrong with your Password !!!"
                print(errorMessage)
            }
        }
    

    the .onSubmit block will execute when the user presses submit button on the keyboard though i would recommend handling this logic inside a view model and execute the logic once everything is set up