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.
Related
I have a simple unbound access 2016 form. On the form, I have several controls including text and combo boxes. On the first text box control I would like to require a data value (underlying table field data type is short text).
I have set the Validation Rule property for the control in the property sheet to "Is Not Null" and added an appropriate validation text message.
For an unknown reason I have not been able to get this validation rule to ever fire. I have cleared the validation rule on the table to make sure it wasn't interfering, however, no luck.
It's like the validation check is not happening when focus leaves the control. There is no other event procedure that has been written that would interfere either.
Thanks for the help.
You can give the control a default value, then
Form_load()
Yourcontrol.setfocus
Sendkeys "{DEL}"
The requirement was to validate the control for a missing value using the validation rule when the control lost focus (ie user tabbed out of text box without ever entering a value). I wanted the user to get immediate feedback that they needed to provide a value for the given control.
As Rene pointed out in the comments, the validation rule does not fire unless there has been a value change.
The Sendkeys solution has issues, I only use Sendkeys as a last resort.
The solution in this case was to put the validation test in the Control_OnExit event handler. The user gets immediate feedback as desired and does not wait until the record is submitted.
One further note that could easily be missed; in an unbound form, the before_update event will never fire.
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 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.
I have a basic model :
class MyModel(models.Model):
my_field = models.CharField()
I have a basic form for this model :
class MyFrom(forms.ModelForm):
class Meta:
model = MyModel
And I have a function that does a basic lookup (a lot more complex in reality, regex etc. won't do) :
POSSIBLE_VALUES = ['aa', 'bb', 'cc', 'dd']
def lookup(some_value):
if some_value in POSSIBLE_VALUES:
# the value is OK, return a string
return some_value
else:
# constructs the 'did you mean' list of suggestions
didyoumean = [pv for pv in POSSIBLE_VALUES if pv in some_value]
# returns a list which might be empty
return didyoumean
Now, the scenario that I want is:
On the site I enter a value in the "my_field" input field and hit submit button.
If the value passes the lookup I should automatically perform the form's action.
If I get multiple possible values then I should display them to the user and no other action is performed.
If I get no answers (an empty list) I should get an error message.
Some additional requirements:
I would prefer the "did you mean" list to be displayed without having to reload the page.
If a user clicks on one of the suggestions I want to perform the form's action without an additional lookup - the value has already been checked.
I want to keep all the logic outside the view and keep it in the form or in the model. This is a must.
I want to avoid hardcoded js in the template and push it into the form if possible. It's not a must.
So I assume that it would all be distributed between this fields validation and a custom widget that would handle the "did you mean" list rendering. I just can't put it all together.
Your help is required :)
EDIT. Ad. 2 in requirements.
That is a basic feature I described. In a more advanced one I want this form to have more fields and so the "did you mean" list should be displayed along with all other fields errors (if any). Then clicking on a hint would just set the my_field's value to it's value without reloading the form. A user would have to correct other errors as well so I can't go to form's action right away. Might there be just some some flag to switch between those two options ("basic" and "advanced").
I would prefer the "did you mean" list to be displayed without
having to reload the page.
Create custom widget, which renders with JS code for checking possible values as user enters it
If a user clicks on one of the suggestions I want to perform the
form's action without an additional
lookup - the value has already been
checked.
Again, that widget, when clicked, should just submit the form.
I want to keep all the logic outside the view and keep it in the
form or in the model. This is a must.
On the form you'll have clean() method to validate everything. If, say, some bogus data passes with submit from p. 2 - you still raise validation error.
I want to avoid hardcoded js in the template and push it into the form if
possible. It's not a must.
Solved with custom widget, details.
I have a user control that displays results from a database in a gridview. The containing page receives querystring values and passes them to the user control to load.
From the containing page, what is the best way to load and display the user control? Currently, I do the following:
Containing Page_Load: Get querystring values
Containing Page_Load: Instantiate User Control
Containing Page_Load: set User Control's properties
Containing Page_Load: Add User Control to Page
I'm not sure what event in the user control to use to hit the database and populate control; should I make a "Populate this Control" Method on the usercontrol and call it from the Containing page? Or use a Page_Load routine in the user control?
If your page is not doing anything to sanitize or process the variables, is there any reason that the user control cannot just grab the values itself?
If you do have a reason to sanitize the data, you could load in the controls' Load event, but in this case since the page appears to be driving the functionality of the user control you might benefit from a "SetupControl" or some other method that you can call after it has been configured.
Override the OnDataBinding method in the user control to access the database and populate the control from the results. You could alternatively have the control handle the DataBinding event.
In other words, treat the user control just like any other control.