2

I have a variable in my NSViewController:

dynamic var roll_rate:Double = 0.0

I attach it to my NSTextField:

enter image description here

Model Key Path shows error, but it is working: When i changed value in field, variable changed too. But what means: Validates Immediately and how do i show and check validation errors for field.

I tried implement method validateRoll_rate, but it didn't call when value changed.

Arti
  • 7,356
  • 12
  • 57
  • 122

1 Answers1

6

Generic solution (work with or without bindings) One way of dealing with this is based on the response here

Basically you use the controlTextDidChange(notification:) delegate method of NSTextField and you implement your validation code in it.

 override func controlTextDidChange (notification: NSNotification) {
    guard let textField = notification.object as? NSTextField else { return }

    // test here, replace the dummy test below with something useful
    if textField.stringValue != "expected value" {
        myTextFieldOutlet.backgroundColor = NSColor.red
        myErrorLabelOutlet.stringValue = "Error !!!"
    } else {
        // everything OK, reset the background color and error label to the normal state
        ....
    }
 }

Obviously myTextFieldOutlet is an outlet linked to your text field and myErrorLabelOutlet is an outlet to a conveniently placed label used to show errors (blank if no error should be presented)

Bindings oriented solution Be sure Validates immediately is selected in Interface Builder and implement the following method in the class where the binding is made (Tuning View Controller in your example)

override func validateValue(_ ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey inKey: String) throws {
    // test here, replace the dummy test below with something useful
    if roll_rate > 10.0 {
        throw NSError(domain: "your-domain", code: 100, userInfo: [NSLocalizedDescriptionKey: "Error, roll rate too high"])
    }
}

When the error is thrown, the user will be presented with the standard sheet announcing the error and the option to cancel the change or correct it.

If Continuously updates value is selected in Interface Builder the method above will be called for each keystroke in the text field, otherwise only after pressing Enter or loosing focus.

Note: For a full understanding on how updating values through bindings work, including what Validates immediately does, see the docs here.

Community
  • 1
  • 1
Bogdan Farca
  • 3,856
  • 26
  • 38
  • nstextfield validation not textView – Leo Dabus Oct 27 '16 at 22:01
  • @LeoDabus is the no something like this? https://github.com/jpotts18/SwiftValidator – Arti Oct 28 '16 at 15:26
  • @bogdanf is the no something like this? https://github.com/jpotts18/SwiftValidator – Arti Oct 28 '16 at 15:26
  • @Arti is my answer helping you ? If yes, please don't forget to accept my answer. – Bogdan Farca Oct 31 '16 at 09:26
  • @bogdanf not sure... I asked about how to show validation errors for field. Yes, a can check in `controlTextDidChange` but how to show labels and make field red. Is there no default class or something to validate fields for cocoa osx. And what does this check box 'Validates immediately' means ? – Arti Oct 31 '16 at 11:49
  • 1
    @Arti I've updated my response to be more useful, I hope. – Bogdan Farca Oct 31 '16 at 13:59