Material: ErrorTextField doesn't reveal errors after upgrading to swift 4.2

Created on 19 Oct 2018  路  9Comments  路  Source: CosmicMind/Material

I have the following code in my app. It used to work fine until the swift 4.2 upgrade.

@IBOutlet weak var usernameTextField: ErrorTextField!

self.usernameTextField.isErrorRevealed = true;
self.usernameTextField.detailLabel.text = "Username or Password is incorrect"

I updated Podfile to the following, but it didn't help

pod 'Material', :git => 'https://github.com/CosmicMind/Material.git', :branch => 'development'
pod 'Motion', :git => 'https://github.com/CosmicMind/Motion.git', :branch => 'development'
good first issue help wanted investigate material question

Most helpful comment

That probably happens since it does not layout errorLabel. You can layout by calling textField.layoutSubviews()

I suggest that instead of textField.errorLabel.text = "blah" use textField.error = "blah", we internally call layoutSubviews

All 9 comments

@OrkhanAlikhanov Thanks for your response. Another change I noticed is that neither detailLabel or errorLabel would show unless the field has been a first responder before. In this case, the error only shows up after the user clicks into the field. How should I resolve this issue?

Interesting. How and where do you set text for labels? Can you send a sample reproducing the issue?

@OrkhanAlikhanov

func submitButtonTapped(sender: UIBarButtonItem) -> Bool {
        var areValid = true
        if (self.firstNameTextField.text?.trimmed.isEmpty)!{
            self.firstNameTextField.errorLabel.text = "First Name required"
            self.firstNameTextField.isErrorRevealed = true
            areValid = false
        }else{
            self.firstNameTextField.isErrorRevealed = false
        }
        if (self.lastNameTextField.text?.trimmed.isEmpty)!{
            self.lastNameTextField.errorLabel.text = "Last Name required"
            self.lastNameTextField.isErrorRevealed = true
            areValid = false
        }else{
            self.lastNameTextField.isErrorRevealed = false
        }
        return areValid
}

Upon tapping on the "submit" button, only "First Name required" shows up.

That probably happens since it does not layout errorLabel. You can layout by calling textField.layoutSubviews()

I suggest that instead of textField.errorLabel.text = "blah" use textField.error = "blah", we internally call layoutSubviews

Thanks so much. It worked! @OrkhanAlikhanov

Awesome!

I believe we should call layoutSubviews internally when isErrorRevealed is set as we were doing before. It was removed in last update, that's why you ran into this issue.

@evagnostic Check out new validator for ErrorTextField #1082. You can set validation rules and error messages on errorTextField.validator and check if they pass using errorTextField.isValid(). There is more detailed information in the pr.

Basic example:

let usernameField = ErrorTextField()
usernameField.placeholder = "Username"
usernameField.validator
  .notEmpty(message: "This field is mandatory")

and then

func didTapSignIn() {
  guard usernameField.isValid() else {
     return // relevant error message is shown automatically.
  }

  // sign user up
} 

Thank you @OrkhanAlikhanov :)

Was this page helpful?
0 / 5 - 0 ratings