I have in my program a textfield which represents a fileName from a model object using bindings. I want that this textField could be used to enter a new fileName and after user presses Enter it should send the message to my model object that value of fileName has changed in my textField, then inside my model object the method for checking if such filename can be used is called. And if it returns true, then it should apply new value to my model object, if not, then value in my textField should undo to initial value.
Does somebody know how it could be implemented? I can validate my value but I can't make my textField refresh to initial value. As for both changing the model object value, and texField refreshing should be used single method which is KVC compliant I don't know how to implement such behaviour.
Any help would be very appreciated.
This doesn't sound like a good UX to me; as a user, I would be upset to find that if I made a simple typo in a text field, it completely erased what I had just input. It would be much better to show a message indicating what was wrong and how the user can fix it. Nonetheless...
I think you should set up a text field delegate. Have the delegate keep a variable holding the last valid string that was input. Then in textFieldShouldEndEditing:, do your check for validity, and if the check does not pass, set the text field's value to that variable and return NO. If it does pass, change the variable to hold the new valid string and return YES.
Related
I have a case where I would like to programmatically assign the first responder to an NSTextField and then programmatically insert a value into the NSTextField as well.
I have this working with the following code:
field.window?.makeFirstResponder(field)
field.stringValue = "Auto generated value"
In the example above, field has a binding to my model. If the user chooses to edit Auto generated value and then hit the return key, the first responder is resigned and the edited value is set on my model. BUT, if the user chooses not to edit Auto generated value and hits the return key, the first responder is resigned but the model is not updated.
It's as if the field is not marked as dirty when programmatically updating the value.
I would like to avoid having to manually update the model when the case above occurs due to some complexities that I have left out in the example. I would like it to apply through whichever binding is set just like it would when the user manually types in the value.
Is this possible?
If you use binding with a NSTextField or any other AppKit control, you should perform programmatically value changes on the data source / model, instead of the other way around by manipulating the control value in code. By doing so, you will not have the problems as described in your question.
Inserting the text in the field editor starts an editing session (undocumented feature).
if window.makeFirstResponder(textField1),
let fieldEditor = textField1.currentEditor() as? NSTextView {
fieldEditor.insertText("Auto generated value", replacementRange: NSRange(location: 0, length: fieldEditor.textStorage?.length ?? 0))
}
I have followed this tutorial and successfully bind my NSTableView to a core data entity.
The table is a view based cell and is populated perfectly from the core data entity.
I have + and - buttons bind to the NSArrayController. When I press the plus button a new record is added to core data and appear on the table, when I select an entry and press the minus sign, that is removed from the database. For that matter I have override this code for the add/remove keys.
#IBAction override func add(_ sender: (Any)?) {
let newApp = self.newObject() as AnyObject
newApp.setValue("New Entry", forKey: "name")
self.addObject(newApp as Any)
}
#IBAction override func remove(_ sender: (Any)?) {
// Do certain stuff before removing all selected rows
self.remove(atArrangedObjectIndexes: self.tableView!.selectedRowIndexes)
}
I made the table view cells editable. When the plus sign button is pressed a new entry is created and appear on the table with the text "New Entry". This new entry creates a core data record. Let's call it record 1.
Now I want the user to edit the entry with the name they want.
So I select the new cell and press enter. The cell is now on edit mode.
I type the new name I want to that cell, that will be passed to the core data entity.
I type, for example, BONOBO and press ENTER.
This table must contain just unique names. So, as soon as ENTER is pressed and the cell ends editing, I want to check core data to see if the name BONOBO is already taken and if it is, reject the name and tell the user to provide a new name.
This is the problem: as soon as I press ENTER, record 1 changes its name instantly from New Entry to BONOBO, before I can check if the entry already exists on the database, meaning that any check I do, will always tell me that the record exists. In fact the record exists in memory because the context was not saved yet.
I am intercepting the enter press by setting the delegate of all cells to a class and using its delegate method controlTextDidEndEditing(_ obj: Notification).
I have also tried to set an action for the textfields but the problem is the same.
How do I intercept it before the core data change happens?
The trick here is to leverage Key-Value Coding's built-in validation methods. See Object Validation.
I've never done this with a managed object, but the process seems to be the same as with regular KVC validation. You want to implement a method with the name:
validate<Key>:error:
... Where 'Key' is the name of the parameter you're trying to validate. This takes in a pointer for the value you want to validate, and another for an NSError object. Inside the method you test whether the passed-in value is acceptable. You can return true to accept it, modify the value and return true to accept a revised version, or return false to reject it outright (modifying the error object to have something to send back to the user).
On an EWF page, is it possible to alter the content of a form item during validation (when a validation fails)? For an example: say you have a text box that you want to be spell checked before it gets entered into the database. You use the modification's GetSpellCheckedWordTextFormItem to get the form item, and you want to replace what the user enters ("teh") with a likely suggestion ("the") when the validation fails to find a word it knows. Then the user sees the validation error ("Is this the word you meant?"), looks at it and corrects it or not, then re-submits.
Is there a way to do that? If so, how?
The specific answer to your question is no, you can't alter any form values if validation fails. To implement this, you'd need to let the validation succeed and let the data get modified. As part of the validation/modification, you could set a piece of page state that causes the next loadData pass to display the "is this the word you meant?" message near the spell-checked form item. Of course, you would have already saved the corrected text.
Alternatively, you could use PostBack.CreateIntermediate to make a post-back that only runs the spell-check, puts the corrected text in page state, and displays "is this the word you meant?". You'd set that post-back to fire when the user tabs out of the text box, and then you'd have the main post-back grab the corrected text from page state and save it in the database or other durable storage.
I have a window with a small text box, bound to a Core Data attribute. The value the user enters into the text box needs to be within certain parameters. These parameters are dynamic. If the entered value is outside of the parameters, a dialog box is displayed asking if the user wants to revert to a previous value, set the value to a minimum, etc. I have implemented the controlTextDidEndEditing method to intercept and validate the value the user enters. My problem is when the user saves or quits. The user can enter a bad value, select save or quit and value is saved, bypassing the validation. Is there a way to have my validation method called before a save? Thanks!
Instead of using the text field delegate, you should instead implement validation in your NSManagedObject subclasses. You can then enforce what values are valid and return an appropriate error message if an invalid value is entered. Doing it this way means that the model is responsible for enforcing validity, which is the logical place to do it.
There is more info about validation in the appropriate section of the Core Data documentation.
Hi guys i am using a dropdownlist which have the value please specify other, when that value is selected in prompts out a textfield which has the value please specify other, but for the user to type in anything in the textfield he has to delete the value manually..i am looking for a code which can delete the value automatically when the user type in the textfield..
Using jQuery, you can make textbox hints. Even better, reuse someone else's code:
http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
Example: http://remysharp.com/wp-content/uploads/2007/01/input_hint.html