0

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)

                            }
  • Please [edit] your question to add the relevant code as text in a code block. See [ask] – Paulw11 Jul 30 '22 at 21:31
  • What is `SecureTextField`? The SwiftUI native component is [`SecureField`](https://developer.apple.com/documentation/swiftui/securefield) the code you are using for `TextField` is deprecated too. You should use an [`onChange(of:)`](https://developer.apple.com/documentation/swiftui/label/onchange(of:perform:)) modifier – Paulw11 Jul 30 '22 at 22:29
  • In a view body you need to return/display other views, your `if validatePassword( ...)` does not return any `View`, so you get the error you show. Put that code in a function and call it from say `.onChange(of:perform:)` attached to your made up `SecureTextField`. – workingdog support Ukraine Jul 30 '22 at 23:08
  • thanks @Paulw11 , my SecureField is where I work the eye Icon: – Jorge Lopez Jul 31 '22 at 14:16

1 Answers1

0

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

Blacksmith
  • 199
  • 7
  • Works perfect now, I just need to execute that after losing the focus on the secureTextField. to use in the same way as I did in the TextField for my email check. Thank you so much for your help! – Jorge Lopez Jul 31 '22 at 14:50