Skip to content

Commit

Permalink
Trim whitespace from STPAddressFieldTableViewCell contents when readi…
Browse files Browse the repository at this point in the history
…ng the property.

Fixes a user-interaction issue (knowingly) introduced by #870.

When selecting email or zip code autocompletion suggestions, they fail validation. US zip
codes would fail immediately, while email addresses waited until the field lost focus.
This was an unpleasant UX experience.

This is both a very concise fix, but also one I like. It keeps the contents of the text
field unchanged, and does not interfere with editing in any way. Leading & trailing spaces
are simply ignored while validating, and they are likewise ignored when considering what
the logical contents of the cell/text field are.

This results in the trimmed string being persisted to the server, and the trimmed string
being passed around the payment context (like copying to the other address type)
  • Loading branch information
danj-stripe committed Dec 20, 2017
1 parent 3c70479 commit 080304a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Stripe/STPAddressFieldTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ @interface STPAddressFieldTableViewCell() <UITextFieldDelegate, UIPickerViewDele

@implementation STPAddressFieldTableViewCell

@synthesize contents = _contents;

- (instancetype)initWithType:(STPAddressFieldType)type
contents:(NSString *)contents
lastInList:(BOOL)lastInList
Expand Down Expand Up @@ -334,6 +336,13 @@ - (NSString *)caption {
return self.textField.placeholder;
}

- (NSString *)contents {
// iOS 11 QuickType completions from textContentType have a space at the end.
// This *keeps* that space in the `textField`, but removes leading/trailing spaces from
// the logical contents of this field, so they're ignored for validation and persisting
return [_contents stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (void)setContents:(NSString *)contents {
_contents = contents;
if (self.type == STPAddressFieldTypeCountry) {
Expand Down

0 comments on commit 080304a

Please sign in to comment.