FXForms icon indicating copy to clipboard operation
FXForms copied to clipboard

Validation and UI feedback for input values

Open jonathantrevor opened this issue 10 years ago • 3 comments

Whats the recommend pattern for validating the field values and providing feedback to the user?

For example, if you have a password update triple of fields (current password, new password and verify new password) you would want to verify that the two new password fields are the same and alert the user of the problem once the user resigns first responder and both have text in.

Similarly, lets say the user enters an invalid email into a template field of emails, how would you:

  • know when the user has finished editing that email field (setValue:forKey I guess?)
  • provide some kind of UI feedback around that specific field?

Is this more custom field work?

jonathantrevor avatar Jan 28 '15 05:01 jonathantrevor

Is there any update on this topic? I'm looking for a way to validate the fields and mark them when not valid.

Many thanks, DAN

ldantona avatar Sep 18 '15 10:09 ldantona

It's been a few months since I've worked with FXForms, so forgive me if I'm missing some details in my answer. I was able to validate input by adding an FXFormFieldAction to the field. You do this by assigning a selector string to the key FXFormFieldAction ("action"). I believe the action will be performed whenever the field is selected and when the first responder is resigned (user navigates out of the field)

Here is an example on validating a date code in the format MMYY.

When building your field...

// Don't forget the colon after the selector string so you can get the field in your method!
[fieldDict setValue:@"checkDateCodeRegEx:" forKey: FXFormFieldAction];

Now create the selector method. I put the method in the view controller that presented the form. From the FXForm README, "...the target is determined by cascading up the responder chain from the cell until an object is encountered that responds to it. That means that you could choose to implement this action method on the tableview, its superview, the view controller, the app delegate, or even the window."

- (void)checkDateCodeRegEx:(id)sender {
    FXFormTextFieldCell *fieldCell = (FXFormTextFieldCell *)sender;
    NSString *inputString = [NSString stringWithFormat:@"%@", fieldCell.field.value];

    NSString *regex = @"^(0[1-9]|1[0-2])[0-9][0-9]$";  // MMYY
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    if ([regexTest evaluateWithObject:inputString] || !inputString || [inputString isEqualToString:@""]) {
        fieldCell.textField.textColor = [UIColor blackColor];
    } else {
        fieldCell.textField.textColor = [UIColor redColor];
    }
}

This will simply evaluate what was inputed into that field, and then change the font color depending on if the input was determined to be invalid or valid.

timgcarlson avatar Sep 18 '15 21:09 timgcarlson

Hi @timgcarlson thanks for your detailed answer!

I'm trying to reproduce what you suggest but my action is never called. For a text field the action seems to be called at line 3001 of FXForms.m, but the textFieldDidEndEditing method is never called. Does it work for you with iOS 9 too?

EDIT: here's the issue: https://github.com/nicklockwood/FXForms/issues/410 which is not related to form validation.

ldantona avatar Sep 21 '15 15:09 ldantona