Skip to content

Commit 93794c9

Browse files
author
Aaron McTavish
committed
Merge branch 'update-validator-controls' into v2_0
2 parents e2e192b + 1ec89d6 commit 93794c9

File tree

10 files changed

+62
-8
lines changed

10 files changed

+62
-8
lines changed

Example/iOS/FormViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class FormViewController: UIViewController {
3939
override func viewDidLoad() {
4040
super.viewDidLoad()
4141

42+
form.addEntry(underlyingView.titleEntry.textField)
4243
form.addEntry(underlyingView.nameEntry.textField)
4344
form.addEntry(underlyingView.emailEntry.textField)
4445

Example/macOS/FormAccessibility.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct FormAccessibility {
1919
static let NameLabel = "NAME_LABEL"
2020
static let NameTextField = "NAME_TEXTFIELD"
2121

22+
static let TitleLabel = "TITLE_LABEL"
23+
static let TitleTextField = "TITLE_TEXTFIELD"
24+
2225
static let SubmitButton = "SUBMIT_BUTTON"
2326
}
2427

Example/macOS/FormView.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@ import FormValidatorSwift
1414
final class FormView: NSView {
1515

1616

17+
// MARK: - Types
18+
19+
private struct Constants {
20+
21+
static let minimumTextFieldWidth: CGFloat = 300.0
22+
23+
}
24+
25+
1726
// MARK: - Properties
1827

28+
let titleEntry = FormEntryView<AlphabeticValidator>()
1929
let nameEntry = FormEntryView<AlphabeticValidator>()
2030
let emailEntry = FormEntryView<EmailValidator>()
2131

@@ -44,6 +54,10 @@ final class FormView: NSView {
4454
stackView.distribution = .fill
4555
addSubview(stackView)
4656

57+
titleEntry.textLabel.stringValue = NSLocalizedString("Title", comment: "")
58+
titleEntry.textField.shouldAllowViolation = true
59+
stackView.addArrangedSubview(titleEntry)
60+
4761
nameEntry.textLabel.stringValue = NSLocalizedString("Surname", comment: "")
4862
stackView.addArrangedSubview(nameEntry)
4963

@@ -60,6 +74,9 @@ final class FormView: NSView {
6074

6175
// Accessibility
6276

77+
titleEntry.textLabel.setAccessibilityIdentifier(FormAccessibility.Identifiers.TitleLabel)
78+
titleEntry.textField.setAccessibilityIdentifier(FormAccessibility.Identifiers.TitleTextField)
79+
6380
nameEntry.textLabel.setAccessibilityIdentifier(FormAccessibility.Identifiers.NameLabel)
6481
nameEntry.textField.setAccessibilityIdentifier(FormAccessibility.Identifiers.NameTextField)
6582

@@ -75,8 +92,9 @@ final class FormView: NSView {
7592
stackView.translatesAutoresizingMaskIntoConstraints = false
7693

7794
NSLayoutConstraint.activate([
78-
nameEntry.widthAnchor.constraint(greaterThanOrEqualToConstant: 300.0),
79-
emailEntry.widthAnchor.constraint(greaterThanOrEqualToConstant: 300.0),
95+
titleEntry.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.minimumTextFieldWidth),
96+
nameEntry.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.minimumTextFieldWidth),
97+
emailEntry.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.minimumTextFieldWidth),
8098

8199
stackView.topAnchor.constraint(equalTo: topAnchor, constant: stackViewMargin),
82100
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -stackViewMargin),

Example/macOS/FormViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class FormViewController: NSViewController {
3939
override func viewDidLoad() {
4040
super.viewDidLoad()
4141

42+
form.addEntry(underlyingView.titleEntry.textField)
4243
form.addEntry(underlyingView.nameEntry.textField)
4344
form.addEntry(underlyingView.emailEntry.textField)
4445

FormValidatorSwift.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
000A5B6E1E1B9AE5001DC2FF /* ConfigurationSeeds.swift in Sources */ = {isa = PBXBuildFile; fileRef = 000A5B6C1E1B9AE5001DC2FF /* ConfigurationSeeds.swift */; };
2626
000D64E91E1F91FD0067B14E /* NumericConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 000D64E81E1F91FD0067B14E /* NumericConfiguration.swift */; };
2727
000D64EA1E1F91FD0067B14E /* NumericConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 000D64E81E1F91FD0067B14E /* NumericConfiguration.swift */; };
28+
00169CDE1E32016400DB3193 /* ValidatorTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88644DA61E2967F7006B0150 /* ValidatorTextFieldTests.swift */; };
29+
00169CDF1E32016500DB3193 /* ValidatorTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88644DA61E2967F7006B0150 /* ValidatorTextFieldTests.swift */; };
30+
00169CE01E32016900DB3193 /* ValidatorTextViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88644DA81E296AEC006B0150 /* ValidatorTextViewTests.swift */; };
31+
00169CE11E32016A00DB3193 /* ValidatorTextViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88644DA81E296AEC006B0150 /* ValidatorTextViewTests.swift */; };
2832
002C04261C454AAB005CC374 /* FormValidatorSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 002C04241C454AAB005CC374 /* FormValidatorSwift.h */; settings = {ATTRIBUTES = (Public, ); }; };
2933
002DDD091E1D2DFB007796C6 /* PostcodeConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 002DDD081E1D2DFB007796C6 /* PostcodeConfiguration.swift */; };
3034
002DDD0A1E1D2DFB007796C6 /* PostcodeConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 002DDD081E1D2DFB007796C6 /* PostcodeConfiguration.swift */; };
@@ -998,6 +1002,7 @@
9981002
isa = PBXSourcesBuildPhase;
9991003
buildActionMask = 2147483647;
10001004
files = (
1005+
00169CDE1E32016400DB3193 /* ValidatorTextFieldTests.swift in Sources */,
10011006
00BB68421C47FDCE00714F34 /* AlphabeticValidatorTests.swift in Sources */,
10021007
00BB683C1C47FDC400714F34 /* PostcodeConditionTests.swift in Sources */,
10031008
BAF88F311DCA566C001845D1 /* CreditCardValidatorTests.swift in Sources */,
@@ -1020,6 +1025,7 @@
10201025
00BB68461C47FDCE00714F34 /* NumericValidatorTests.swift in Sources */,
10211026
00BB68341C47FDC400714F34 /* AndConditionTests.swift in Sources */,
10221027
00BB68431C47FDCE00714F34 /* AlphanumericValidatorTests.swift in Sources */,
1028+
00169CE01E32016900DB3193 /* ValidatorTextViewTests.swift in Sources */,
10231029
00BB683F1C47FDC400714F34 /* URLConditionTests.swift in Sources */,
10241030
00BB684C1C47FDCE00714F34 /* URLShorthandValidatorTests.swift in Sources */,
10251031
00BB68391C47FDC400714F34 /* EmailConditionTests.swift in Sources */,
@@ -1178,6 +1184,7 @@
11781184
isa = PBXSourcesBuildPhase;
11791185
buildActionMask = 2147483647;
11801186
files = (
1187+
00169CDF1E32016500DB3193 /* ValidatorTextFieldTests.swift in Sources */,
11811188
007244451E1FB9CD0050DFE9 /* EmailConditionTests.swift in Sources */,
11821189
007244441E1FB9CD0050DFE9 /* CreditCardConditionTests.swift in Sources */,
11831190
007244571E1FB9D60050DFE9 /* PresentValidatorTests.swift in Sources */,
@@ -1200,6 +1207,7 @@
12001207
007244531E1FB9D60050DFE9 /* EmailValidatorTests.swift in Sources */,
12011208
007244561E1FB9D60050DFE9 /* PostcodeValidatorTests.swift in Sources */,
12021209
0072445A1E1FB9D60050DFE9 /* URLShorthandValidatorTests.swift in Sources */,
1210+
00169CE11E32016A00DB3193 /* ValidatorTextViewTests.swift in Sources */,
12031211
007244431E1FB9CD0050DFE9 /* CreditCardTypeTests.swift in Sources */,
12041212
007244591E1FB9D60050DFE9 /* URLValidatorTests.swift in Sources */,
12051213
007244521E1FB9D60050DFE9 /* CompositeValidatorTests.swift in Sources */,

Sources/Controls/ValidatorTextField-AppKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class ValidatorTextField: NSTextField, ValidatorControl {
1414

1515
// MARK: - Properties
1616

17-
open var shouldAllowViolation = true
17+
open var shouldAllowViolation = false
1818
open var validateOnFocusLossOnly = false
1919
open let validator: Validator
2020
/// Validator delegate for the text field.
@@ -197,7 +197,7 @@ fileprivate final class ValidityFormatter: Formatter {
197197

198198
if !validatorTextField.validateOnFocusLossOnly && origSelRange.location != 0,
199199
let conditions = conditions,
200-
(!validatorTextField.shouldAllowViolation || conditions[0].shouldAllowViolation) {
200+
(!validatorTextField.shouldAllowViolation || !(conditions.isEmpty || conditions[0].shouldAllowViolation)) {
201201

202202
return false
203203
}

Sources/Controls/ValidatorTextField-UIKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ internal class ValidatorTextFieldResponder: NSObject, UITextFieldDelegate {
177177

178178
if !validatorTextField.validateOnFocusLossOnly && range.location != 0,
179179
let conditions = conditions,
180-
(!validatorTextField.shouldAllowViolation || !conditions[0].shouldAllowViolation) {
180+
(!validatorTextField.shouldAllowViolation || !(conditions.isEmpty || conditions[0].shouldAllowViolation)) {
181181
return false
182182
}
183183

Sources/Controls/ValidatorTextView-AppKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class ValidatorTextView: NSTextView, ValidatorControl {
1414

1515
// MARK: - Properties
1616

17-
open var shouldAllowViolation = true
17+
open var shouldAllowViolation = false
1818
open var validateOnFocusLossOnly = false
1919
open let validator: Validator
2020
open weak var validatorDelegate: ValidatorControlDelegate?
@@ -66,7 +66,7 @@ open class ValidatorTextView: NSTextView, ValidatorControl {
6666

6767
if !validateOnFocusLossOnly && affectedCharRange.location != 0,
6868
let conditions = conditions,
69-
(!shouldAllowViolation || conditions[0].shouldAllowViolation) {
69+
(!shouldAllowViolation || !(conditions.isEmpty || conditions[0].shouldAllowViolation)) {
7070

7171
return false
7272
}

Sources/Controls/ValidatorTextView-UIKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ internal class ValidatorTextViewResponder: NSObject, UITextViewDelegate {
130130

131131
if !validatorTextView.validateOnFocusLossOnly && range.location != 0,
132132
let conditions = conditions,
133-
(!validatorTextView.shouldAllowViolation || conditions[0].shouldAllowViolation) {
133+
(!validatorTextView.shouldAllowViolation || !(conditions.isEmpty || conditions[0].shouldAllowViolation)) {
134134
return false
135135
}
136136

Tests/UI Tests/macOS/FormTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ class FormTests: XCTestCase {
3939
}
4040

4141
func testTextField_AllowedViolations() {
42+
let app = XCUIApplication()
43+
let expectedResult = "Foo12"
44+
45+
let titleTextField = app.textFields[FormAccessibility.Identifiers.TitleTextField]
46+
titleTextField.click()
47+
titleTextField.typeText(expectedResult)
48+
49+
let actualResult = titleTextField.value as? String
50+
51+
XCTAssertEqual(actualResult, expectedResult, "The text field should have text \(expectedResult) but received \(actualResult).")
52+
}
53+
54+
func testTextField_AllowedViolations_and_validateOnFocusLossOnly() {
4255
let app = XCUIApplication()
4356
let expectedResult = "Foo12 ?"
4457

@@ -54,13 +67,18 @@ class FormTests: XCTestCase {
5467
func testForm_Valid() {
5568
let app = XCUIApplication()
5669

70+
let title = "Developer"
5771
let name = "Foo"
5872
let email = "[email protected]"
5973

74+
let titleTextField = app.textFields[FormAccessibility.Identifiers.TitleTextField]
6075
let nameTextField = app.textFields[FormAccessibility.Identifiers.NameTextField]
6176
let emailTextField = app.textFields[FormAccessibility.Identifiers.EmailTextField]
6277
let submitButton = app.buttons[FormAccessibility.Identifiers.SubmitButton]
6378

79+
titleTextField.click()
80+
titleTextField.typeText(title)
81+
6482
nameTextField.click()
6583
nameTextField.typeText(name)
6684

@@ -79,13 +97,18 @@ class FormTests: XCTestCase {
7997
func testForm_Invalid() {
8098
let app = XCUIApplication()
8199

100+
let title = "Developer"
82101
let name = "Foo"
83102
let email = "Bar"
84103

104+
let titleTextField = app.textFields[FormAccessibility.Identifiers.TitleTextField]
85105
let nameTextField = app.textFields[FormAccessibility.Identifiers.NameTextField]
86106
let emailTextField = app.textFields[FormAccessibility.Identifiers.EmailTextField]
87107
let submitButton = app.buttons[FormAccessibility.Identifiers.SubmitButton]
88108

109+
titleTextField.click()
110+
titleTextField.typeText(title)
111+
89112
nameTextField.click()
90113
nameTextField.typeText(name)
91114

0 commit comments

Comments
 (0)