UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Disabling a button based on the state of two booleans

Forums > SwiftUI

Hi all,

I have some code like this:

                            Button("Continue") {
                                viewModel.sendVerificationCode()
                            }
                            .disabled(!(viewModel.allPasswordCondsMet && viewModel.isPhoneValid))
                            .buttonStyle(OrangeButton())
                            .padding()
                            .navigationDestination(isPresented: $viewModel.goToVerify) {
                                PhoneVerificationView(verificationID: $viewModel.verificationID, otp: $viewModel.otp, phoneNum: $viewModel.phone)
                            }

However, this button is not disabled when the two booleans are true, instead it's only disabled when one or the other is true. How can I fix this?

   

@Vendeta may be confusing not True and not False logic?

this button is not disabled when the two booleans are true, instead it's only disabled when one or the other is true

Do you not want to undo the reversal of your non-negating language?

Yes. That was confusing on purpose, to make a point.

I recommend you rename your variables to be more expressive. Or add additional computed variables to your struct to clearly define what you do, or do not want to see.

Try this in Playgrounds.

// Make your logic clear. Your password matches your validation rules, or it doesn't.
var isInvalidPasswordFormat  = false  // or true. try both in Playgrounds

// Your phone number passes your validation rules, or it doesn't.
var isInvalidPhoneFormat     = false  // or true

// Both the password and the phone number are invalid formats, or they're not
var somethingNotValid: Bool {
    // somethingNotValid IS TRUE if either
    isInvalidPhoneFormat || isInvalidPasswordFormat
}

var messageToUser: String {
    // if something is NOT VALID show the first message.
    somethingNotValid ? "Phone or Password is not valid." : "Both Phone and Password adhere to formats."
}

// Declare WHAT you want to see. Be very clear!
print( messageToUser )

You can use the computed var somethingNotValid in the button's .disabled() modifier. You're telling SwiftUI that you want the button disabled if somethingNotValid. I think this is very clear logic.

Keep Coding

Please return here with your updated code. Share how you solved your issue.

   

.disabled(!(viewModel.allPasswordCondsMet && viewModel.isPhoneValid))

The logic is confusing and even you might be a bit confused. I'm guessing you want the Continue button to be ENABLED when all password conditions are met and the phone number is valid. But you're saying the opposite.

My guess is the logic works.

You want to restate this to read more like how your mind would read it:

.disabled(!viewModel.allPasswordCondsMet || !viewModel.isPhoneValid)

"Disable the button if the password conditions aren't met OR the phone number isn't valid." It makes my brain feel so much better.

   

I would put in the ViewModel a computed property that will return true if any of the conditions are not valid, however does not take away from logic for each condition will look correct.

var isDisabled: Bool {
    allPasswordCondsMet == false || isPhoneValid == false
}

then add

.disabled(viewModel.isDisabled)

This make it easier to understand from call site and the logic is in the ViewModel

You can !allPasswordCondsMet || !isPhoneValid but it not so clear (my opinion)

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.