Skip to content

Commit

Permalink
Add required field validation property
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanraja committed Jul 31, 2020
1 parent 6db8649 commit bf7780c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Example/FloatingLabelTextFieldSwiftUI/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ContentView: View {
.addValidations([.init(condition: firstName.isValid(.alphabet), errorMessage: "Invalid Name"),
.init(condition: firstName.count >= 2, errorMessage: "Minimum two character long")
])
.isRequiredField(true, with: "Name field is required")
.floatingStyle(ThemeTextFieldStyle())
.modifier(ThemeTextField())

Expand Down Expand Up @@ -102,6 +103,8 @@ struct ContentView: View {
}) {

}
.isShowError(true)
.isRequiredField(true, with: "Password field is required")
.rightView({
Button(action: {
withAnimation {
Expand Down
41 changes: 34 additions & 7 deletions Sources/FloatingLabelTextFieldSwiftUI/FloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ public struct FloatingLabelTextField: View {
@Binding private var textFieldValue: String
@State fileprivate var isSelected: Bool = false
@Binding private var validtionChecker: Bool

private var currentError: TextFieldValidator {
if notifier.isRequiredField && isShowError && textFieldValue.isEmpty {
return TextFieldValidator(condition: false, errorMessage: notifier.requiredFieldMessage)
}

if let firstError = notifier.arrValidator.filter({!$0.condition}).first {
return firstError
}
return TextFieldValidator(condition: true, errorMessage: "")
}

@State fileprivate var isShowError: Bool = false

//MARK: Observed Object
@ObservedObject private var notifier = FloatingLabelTextFieldNotifier()

Expand Down Expand Up @@ -61,6 +68,7 @@ public struct FloatingLabelTextField: View {
SecureField("", text: $textFieldValue.animation()) {
}
.onTapGesture {
self.isShowError = self.notifier.isRequiredField
self.validtionChecker = self.currentError.condition
self.editingChanged(self.isSelected)
if !self.isSelected {
Expand All @@ -71,6 +79,7 @@ public struct FloatingLabelTextField: View {
self.isSelected = self.notifier.isSecureTextEntry
currentTextField.addAction(for: .editingDidEnd) {
self.isSelected = false
self.isShowError = self.notifier.isRequiredField
self.commit()
}
}
Expand All @@ -85,8 +94,10 @@ public struct FloatingLabelTextField: View {
self.isSelected = isChanged
self.validtionChecker = self.currentError.condition
self.editingChanged(isChanged)
self.isShowError = self.notifier.isRequiredField

}, onCommit: {
self.isShowError = self.notifier.isRequiredField
self.validtionChecker = self.currentError.condition
self.commit()
})
Expand All @@ -97,6 +108,15 @@ public struct FloatingLabelTextField: View {
}
}

// MARK: Top error and title lable view
var topTitleLable: some View {
Text((self.currentError.condition || !notifier.isShowError) ? placeholderText : self.currentError.errorMessage)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: notifier.textAlignment.getAlignment())
.animation(.default)
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (self.isSelected ? notifier.selectedTitleColor : notifier.titleColor) : notifier.errorColor)
.font(notifier.titleFont)
}

// MARK: Bottom Line View
var bottomLine: some View {
Divider()
Expand All @@ -108,13 +128,13 @@ public struct FloatingLabelTextField: View {
VStack () {
ZStack(alignment: .bottomLeading) {

Text((self.currentError.condition || !notifier.isShowError) ? placeholderText : self.currentError.errorMessage)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: notifier.textAlignment.getAlignment())
.animation(.default)
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (self.isSelected ? notifier.selectedTitleColor : notifier.titleColor) : notifier.errorColor)
.padding(.bottom, CGFloat(!textFieldValue.isEmpty ? notifier.spaceBetweenTitleText : 0))
.opacity(textFieldValue.isEmpty ? 0 : 1)
.font(notifier.titleFont)
//Top error and title lable view
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty {
self.topTitleLable.padding(.bottom, CGFloat(notifier.spaceBetweenTitleText)).opacity(1)

} else {
self.topTitleLable.padding(.bottom, CGFloat(!textFieldValue.isEmpty ? notifier.spaceBetweenTitleText : 0)).opacity((textFieldValue.isEmpty) ? 0 : 1)
}

HStack {
// Left View
Expand Down Expand Up @@ -303,6 +323,13 @@ extension FloatingLabelTextField {
notifier.errorColor = color
return self
}

/// Sets the field is required or not with message.
public func isRequiredField(_ required: Bool, with message: String) -> Self {
notifier.isRequiredField = required
notifier.requiredFieldMessage = message
return self
}
}

//MARK: Preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ class FloatingLabelTextFieldNotifier: ObservableObject {
@Published var isShowError: Bool = false
@Published var errorColor: Color = .red
@Published var arrValidator: [TextFieldValidator] = []
@Published var isRequiredField: Bool = false
@Published var requiredFieldMessage: String = ""
}

0 comments on commit bf7780c

Please sign in to comment.