open-bytes / swiftuiformvalidator Goto Github PK
View Code? Open in Web Editor NEWDeclarative form validator for SwiftUI.
License: Other
Declarative form validator for SwiftUI.
License: Other
Hi,
This is a great library by the way but I am just trying to work out the following, consider the following validation
lazy var firstNameValidation: ValidationContainer = {
let validators: [StringValidator] = [
PrefixValidator(prefix: "mr."),
CountValidator(count: 6, type: .greaterThanOrEquals)
]
return $firstName.allValid(validators: validators, form: form)
}()
Based on the sequence of the validation in the array. We should display the error message that is associated with the PrefixValidator first, then if it is satisfied we should then display the error message of the second CountValidator.
At the moment the implementation of func allValid(
is setting the error message to be only the required one.
errorMessage: errorMessage().orIfEmpty(form.messages.required),
I think for allValid we should let it workout which error message needs to be displayed. To be honest I tried to modify the public static func create( which is relevant to allValid to set the error message dynamically as we are looping thru the validators but it was slightly tricky.
If there is a simpler way that I am not aware of to do display multiple error messages for the one field (1 at a time) please shed some light.
Thank you.
Hi!
I am trying to validate a picker and am a little stuck. I use the following inside the form
@FormField(inlineValidator: { value in
guard value < 0 || value > 1 else { return "Error" }
return nil
})
@Published var percent: Float = 0
lazy var percentValidation = _percent.validation(manager: manager, disableValidation: {
self.amount != 0
})
and the picker is defined as follows:
Picker("", selection: self.$value) {
ForEach(percentages, id: \.self) {
Text($0, format: .percent).tag($0)
}
}
.validate(validationContainer)
The problem is the Published inside the Formfield property wrapper. Without it the picker does not get updated and with it, I cannot use the validation, as the wrappedValues do not match.
Hi, I'm facing an error while using the SwiftUIFormValidator library during Azure DevOp Pipeline building process.
I'm currently developing an app using Kotlin MultiPlatform Mobile.
I'm using SwiftUIFormValidator for form validations in iOS.
I'm able to build it successfully in local machine but not in Azure DevOps Pipeline.
Here's the error log.
Running script '[CP-User] Build shared' โธ Processing SwiftUIFormValidator-Info.plist ** BUILD FAILED **
The following build commands failed: PhaseScriptExecution [CP-User]\ Build\ shared /Users/runner/Library/Developer/Xcode/DerivedData/iosApp-dongzyrxhgtfzxexzqjllvyjwgcu/Build/Intermediates.noindex/Pods.build/Release-iphoneos/shared.build/Script-4552119A071AC6BAB7327E6434237EC3.sh (in target 'shared' from project 'Pods')
Kindly let me know the solution for the above error. Thank you.
Great framework - thanks for providing this.
Two issues:
for #1, after the textfield font size is increased, the error message size is very small, hard to read - how can this be increased ?
for #2, some custom hacks have been done to fix the typical logic for password and confirm password - such as:
so far, this is not performing properly ...
attached below is the screen shot example of the issues of font sizes:
Is there any way to create an OR validation (enter field A or field B). Currently all validators are AND conjunctions.
Hi, I am facing an issue where the validation function is being triggered multiple times upon any change in the input field. Specifically, my print
statement within the inline validation function is being executed 8 times with every change in the input. I have conducted further debugging to ensure that the issue is not on my end. I checked for any multiple initializations or other common pitfalls that might lead to the validation function being triggered excessively. Despite these checks, the issue persists, and the validation function continues to be called 8 times on every input change. I have also ensured that there are no other dependencies or external factors within my project that could be causing this behavior.
final class SerialNumberFormManager: ObservableObject {
@Published var manager = FormManager(validationType: .immediate)
@FormField(inlineValidator: { value in
print(value)
return nil
})
var serialNumber: String = ""
lazy var serialNumberValidation = _serialNumber.validation(manager: manager)
}
I'm trying to validate a decimal, however all the validators require strings.... (apart from the date validator)
My textfield requires a decimal....
TextField(
"My text",
value: $viewModel.amount,
format: .currency(code: Locale.current.currencyCode ?? "")
)
.keyboardType(.decimalPad)
Could the validators be changed to use generics perhaps ?
The validators also require a published variable.
I'd like to validate the the value is positive or negative, depending on state. e.g. income or expense.
Also that the value isn't zero.
I'm not sure how to proceed, any suggestions?
I have a custom toggle style that displays an empty circle when not enabled and a checked circle when enabled.
struct CheckToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
Button {
configuration.isOn.toggle()
} label: {
Label {
configuration.label
} icon: {
Image(systemName: configuration.isOn ? "checkmark.circle.fill" : "circle")
.foregroundColor(configuration.isOn ? .accentColor : .secondary)
.accessibility(label: Text(configuration.isOn ? "Checked" : "Unchecked"))
.imageScale(.large)
}
}
.buttonStyle(PlainButtonStyle())
}
}
This used to work fine, but after upgrading to the 1.0 version, the checkmark no longer displays when enabled. You can easily test this out in the Example Form by adding the following in ExampleForm:
@FormField(inlineValidator: { value in
return value ? nil : "You must acknowledge"
})
var ack: Bool = false
lazy var ackValidation = _ack.validation(manager: manager)
And the following in one of the sections of ContentView:
Toggle(isOn: $form.ack) {
Text("Random Acknowledgement")
}
.validation(form.ackValidation)
.toggleStyle(CheckToggleStyle())
This worked fine when using versions prior to 1.0. Any ideas?
I'm migrating to the 1.0 version of the library and I'm a bit stuck. I have a couple of validations that previously used inline validations. For example, I have a form that requires the user to enter the email twice and compare to make sure they are the same. It appears that I may need to follow the pattern setup with PasswordFormField and create a custom Form Field and a custom Validatable. That seems like a lot of work.
Am I missing something?
Hello, firstly I would like to thank you and say really nice work.
I came up to something really odd. I created a form in SwiftUI for iPhone that has 10 inputs and it takes more space than the screen size so 3-4 input are not visible and you need to scroll to see them.
The issue:
If I try to edit an existing record and change the first input and try to save, the triggerValidation fails. if I scroll down to see all inputs and then try triggerValidation again it works.
What I tried to verify this:
.onReceive(formInfo.form.$validationMessages) { messages in print(messages) } .onReceive(formInfo.form.$allValid) { isValid in print("isValid", isValid) }
I can see that all validation messages are empty and validation fails. With the isValid print I am able to see when the validation is changed to true. And as I scroll down it goes to true as soon as the last input appears on screen.
I added an array of validations how is it:
lazy var firstNameValidation: ValidationContainer = {
let validators: [StringValidator] = [
CountValidator(count: 6, type: .greaterThanOrEquals),
PrefixValidator(prefix: "st.")
]
return $username.allValid(validators: validators, form: form)
}()
and run manual trigger validation, then the message error not show
form.triggerValidation()
my form
@published var form = FormValidation(validationType: .immediate)
only show error when value changed
I'm trying to setup a multilingual app with Localizable.strings
. So far, the UI elements in SwiftUI all translate as expected. How do we get the validation messages to translate accordingly?
The code and explanation for the first example on the README page is as follows:
// 1
import FormValidator
class ExampleForm: ObservableObject {
// 2
@Published
var manager = FormManager(validationType: .immediate)
// 3
@FormField(validator: NonEmptyValidator(message: "This field is required!"))
var firstName: String = ""
}
struct ContentView: View {
@ObservedObject var form = ExampleForm()
@State var isSaveDisabled = true
var body: some View {
Section(header: Text("Required Fields Validation")) {
TextField("First Name", text: $form.firstName)
.validation(form.firstNameValidation) // WHAT IS THIS?
}
}
}
That code contains:
.validation(form.firstNameValidation)
But form.firstNameValidation does not exist in the example!
So either there's a typo and this is meant to be .validation(form.firstName)
, or additional code is needed, in which case your example is misleading and it's not as "concise and simple" as it's made out to be.
The example should show working code.
Invalid Exclude '/Users/me/Library/Developer/Xcode/DerivedData/snip/SourcePackages/checkouts/
SwiftUIFormValidator/Sources/Examples': File not found.
Invalid Exclude '/Users/me/Library/Developer/Xcode/DerivedData/snip/SourcePackages/
checkouts/SwiftUIFormValidator/Sources/Tests': File not found.
I've added the package and I'm getting these warnings...
I'm using Xcode 13.2.1
I think it's just that the Sources
folder isn't required..
Tests
and Examples
appear in the parent folder.
Best regards,
Jules.
Hi There
Thanks for sharing a great project. Would it be possible to allow a view to handle its own validation UI via a binding instead of putting an error message underneath view via the viewmodifier? For my use case I would like to change the text colour of the input and apply a red stroke around it in addition to showing the error message underneath.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.