Skip to content

Commit

Permalink
Validation logic: Added IASKValidationResultOkWithReplacement
Browse files Browse the repository at this point in the history
indicating that the validation is OK with the replacement performed;
this is useful to auto-fix wrong content (the sample app adds an @ as the account name prefix);
  • Loading branch information
futuretap committed Oct 21, 2024
1 parent cb414c7 commit c4c13be
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion InAppSettingsKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'InAppSettingsKit'
s.version = '3.8.2'
s.version = '3.8.3'
s.summary = 'This iPhone framework allows settings to be in-app in addition to being in the Settings app.'

s.description = <<-DESC
Expand Down
16 changes: 16 additions & 0 deletions InAppSettingsKitSampleApp/Classes/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ extension MainViewController: IASKSettingsDelegate {
}
textField.textColor = .red
return .failed
} else if key == "account_name", let value = textField.text {
let regex = "^@?[\\w](?!.*?\\.{2})[\\w.]{1,28}[\\w]$"
if value.isEmpty {
return .ok
} else if value == "@" {
replacement?.pointee = "" as NSString
return .failed
} else if value.range(of: regex, options: .regularExpression) == nil {
if let previousValue {
replacement?.pointee = previousValue as NSString
return .failedWithShake
}
} else if !value.hasPrefix("@") {
replacement?.pointee = "@\(value)" as NSString
return .okWithReplacement
}
}
return .ok
}
Expand Down
12 changes: 12 additions & 0 deletions InAppSettingsKitSampleApp/Settings.bundle/Complete.plist
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@
<key>IASKCellImage</key>
<string>mail</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Account</string>
<key>Key</key>
<string>account_name</string>
<key>AutocorrectionType</key>
<string>No</string>
<key>IASKCellImage</key>
<string>person.crop.circle</string>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,9 @@ - (void)textFieldDidEndEditing:(IASKTextField *)textField {
};

switch (result) {
case IASKValidationResultOkWithReplacement:
restoreText();
// fallthrough
case IASKValidationResultOk: {
if (![self.settingsStore objectForSpecifier:specifier] && textField.text.length == 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,16 @@ shouldPresentMailComposeViewController:(MFMailComposeViewController*)mailCompose

#pragma mark - Validation
typedef NS_ENUM(NSUInteger, IASKValidationResult) {
/** validation is OK, no replacement is performed. */
IASKValidationResultOk,

/** validation is OK with the replacement performed. */
IASKValidationResultOkWithReplacement,

/** validation has failed, the replacement is performed. */
IASKValidationResultFailed,

/** validation has failed, the replacement is performed, and the field shakes to indicate the error. */
IASKValidationResultFailedWithShake,
};
/** validate user input in text fields
Expand Down

0 comments on commit c4c13be

Please sign in to comment.