Skip to content

Commit 117ff85

Browse files
author
Ahmet Sina Ustem
committed
Validation delegate methods added.
1 parent fd117ee commit 117ff85

4 files changed

Lines changed: 47 additions & 13 deletions

File tree

ALFormInput.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'ALFormInput'
11-
s.version = '0.2.2'
11+
s.version = '0.2.3'
1212
s.summary = 'Commonly used form inputs in our projects'
1313

1414
s.homepage = 'https://github.com/applogistdev/ALFormInput'

ALFormInput/Classes/ALValidatableTextField.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ALValidatableTextField: SkyFloatingLabelTextFieldWithIcon {
3131
}
3232
}
3333

34+
public var isValidSuccess: Bool = false
3435

3536
/// Check your free FontAwesome icon in https://fontawesome.com/icons?d=gallery
3637
public var fontAwesomeImage: FontAwesome? {
@@ -263,18 +264,6 @@ public class ALValidatableTextField: SkyFloatingLabelTextFieldWithIcon {
263264
fontAwesomeStyle = .solid
264265
}
265266

266-
func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
267-
// turn the fields to red
268-
for (field, error) in errors {
269-
if let field = field as? UITextField {
270-
field.layer.borderColor = config.errorColor.cgColor
271-
field.layer.borderWidth = 1.0
272-
}
273-
error.errorLabel?.text = error.errorMessage // works if you added labels
274-
error.errorLabel?.isHidden = false
275-
}
276-
}
277-
278267
/// Gets called when editing starts on a paddingNeeded enabled textfield
279268
@objc private func setPaddingForEditingTextFields() {
280269
let editingPad = UIEdgeInsets(top: 8, left: 0, bottom: -8, right: 0)
@@ -285,6 +274,27 @@ public class ALValidatableTextField: SkyFloatingLabelTextFieldWithIcon {
285274
private func setPadding(_ padding: UIEdgeInsets = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)) {
286275
self.padding = padding
287276
}
277+
278+
public func validate() {
279+
validator.validate(self)
280+
}
281+
}
282+
283+
// MARK: - ValidationDelegate
284+
extension ALValidatableTextField: ValidationDelegate {
285+
public func validationSuccessful() {
286+
isValidSuccess = true
287+
}
288+
289+
public func validationFailed(_ errors: [(Validatable, ValidationError)]) {
290+
// turn the fields to red
291+
isValidSuccess = false
292+
for (field, error) in errors {
293+
if let field = field as? SkyFloatingLabelTextFieldWithIcon {
294+
field.errorMessage = error.errorMessage
295+
}
296+
}
297+
}
288298
}
289299

290300
// MARK: - TextField Padding
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// AL+Collection.swift
3+
// ALFormInput_Example
4+
//
5+
// Created by AppLogist on 20.04.2020.
6+
// Copyright © 2020 CocoaPods. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Collection where Element: ALValidatableTextField {
12+
func validateAll() {
13+
forEach({ $0.validate() })
14+
}
15+
16+
var isAllSuccess: Bool {
17+
validateAll()
18+
return allSatisfy({ $0.isValidSuccess })
19+
}
20+
}

Example/ALFormInput.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
158CF6E85C91D42E4F7576AA /* Pods_ALFormInput_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D73D2CB621F119056B949E7 /* Pods_ALFormInput_Tests.framework */; };
11+
35365FA5244DB5A2001C18C9 /* AL+Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35365FA4244DB5A2001C18C9 /* AL+Collection.swift */; };
1112
359DA55724472FE400B1F64C /* ALValidatableTextFieldWithIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 359DA55624472FE400B1F64C /* ALValidatableTextFieldWithIcon.swift */; };
1213
35ACD48C2445D3180087512C /* ALDatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35ACD48B2445D3180087512C /* ALDatePicker.swift */; };
1314
35ACD48E2445D8A70087512C /* AL+ActionSheetPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35ACD48D2445D8A70087512C /* AL+ActionSheetPicker.swift */; };
@@ -46,6 +47,7 @@
4647
/* Begin PBXFileReference section */
4748
15C06C9630A5ECBC5A9FC89F /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
4849
16E4DB1FA5E21E933A621C5C /* Pods-ALFormInput_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ALFormInput_Tests.release.xcconfig"; path = "Target Support Files/Pods-ALFormInput_Tests/Pods-ALFormInput_Tests.release.xcconfig"; sourceTree = "<group>"; };
50+
35365FA4244DB5A2001C18C9 /* AL+Collection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AL+Collection.swift"; sourceTree = "<group>"; };
4951
359DA55624472FE400B1F64C /* ALValidatableTextFieldWithIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ALValidatableTextFieldWithIcon.swift; sourceTree = "<group>"; };
5052
35ACD48B2445D3180087512C /* ALDatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ALDatePicker.swift; sourceTree = "<group>"; };
5153
35ACD48D2445D8A70087512C /* AL+ActionSheetPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AL+ActionSheetPicker.swift"; sourceTree = "<group>"; };
@@ -125,6 +127,7 @@
125127
35DBD20624459AE3008A8144 /* AL+String.swift */,
126128
35DBD20724459AE3008A8144 /* AL+UITextField.swift */,
127129
35ACD48D2445D8A70087512C /* AL+ActionSheetPicker.swift */,
130+
35365FA4244DB5A2001C18C9 /* AL+Collection.swift */,
128131
);
129132
path = Extensions;
130133
sourceTree = "<group>";
@@ -439,6 +442,7 @@
439442
35DBD22E24459AE3008A8144 /* AL+UITextField.swift in Sources */,
440443
35DBD24A24459AE3008A8144 /* ALValidatableConfig.swift in Sources */,
441444
35DBD24624459AE3008A8144 /* TCIdentityRule.swift in Sources */,
445+
35365FA5244DB5A2001C18C9 /* AL+Collection.swift in Sources */,
442446
35DBD22D24459AE3008A8144 /* AL+String.swift in Sources */,
443447
35DBD23B24459AE3008A8144 /* ReplacedExactLengthRule.swift in Sources */,
444448
35DBD22524459AE3008A8144 /* ALValidatableTextFieldType.swift in Sources */,

0 commit comments

Comments
 (0)