Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Validator. #157

Closed
wants to merge 1 commit into from
Closed

Added Validator. #157

wants to merge 1 commit into from

Conversation

Matic1993
Copy link

Added some default validation.

User can also add custom regex for validation.

@k0nserv
Copy link
Contributor

k0nserv commented Jul 22, 2017

Hey @Matic1993,

I think this should be more general. Instead of having a single protocol with many checks it should be a protocol with a single unified interface.

typealias Valdiator = (text: String) -> (Bool, String)?

and then concrete implementation of validators that have the above signature for example

func validateAlphabeticalValue(text: String) -> (Bool, String)? {
    let valid = !text!.isEmpty && text!.rangeOfCharacter(from: CharacterSet.letters.inverted) == nil;

    guard valid else {
        return (false, "Invalid number")
    }

    return nil
}

@Matic1993
Copy link
Author

Hey @k0nserv

thank you for your suggestion - do you know for any tutorial where I can check your idea?

I will implement your suggestion.

Thank you for your help.

@k0nserv
Copy link
Contributor

k0nserv commented Jul 25, 2017

So the idea is that each instance of SkyFloatingLabelTextField will have an optional validator function(Validator?).

typealias Validator = (_ text: String) -> ValidationResult

ValidationResult is just a simple enum encapsulating either that the value is valid or an error with a string to describe the error.

enum ValidationResult {
    case ok
    case error(String)
}

Everytime the text changes we can run the Validator if it exists and then set the errorMessage as appropriate. We need to make sure we listen to text changes in a transparent way that still lets the consumer set a custom delegate. See attached playground

Validator.playground.zip

@k0nserv
Copy link
Contributor

k0nserv commented Nov 16, 2018

Closing due to inactivity. Feel free to reopen or open a new PR if you wish to continue working on this

@k0nserv k0nserv closed this Nov 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants