قالب وردپرس درنا توس
Home / IOS Development / # 279: Swiftier Forms with Form ValidatorSwift 📑 – Little Bites of Cocoa

# 279: Swiftier Forms with Form ValidatorSwift 📑 – Little Bites of Cocoa



Subjects

Topics

Validating data is usually one of the "renderings" we encounter when building apps. It can be annoying and boring code to write and can be a great source of mistakes. Today we are going to check out a great library called FormValidatorSwift that can help us avoid all these issues. Let's have a look.

The core of FormValidatorSwift is built around to check if a String passes a kind of test that is to be considered "valid."

We use individual Terms or combinations of Terms called Validators . We add these to controls like Text Fields and Text Views to allow them to be validated. [1

9659000] Let's try the enclosed ValidatorTextField to validate a

:

  la    field    =    ValidatorTextField   (  Validator  :    EmailValidator   ()) 

Then we configure it to allow the user to begin typing in an email address that is invalid as "hey @" and specify that it should only be validated when it loses focus.

  field .   shouldAllowViolation    =    true 
  field .   validateOnFocusLossOnly    =    true 

This sets us perfectly for our last step: Displays the validation information. [19659000] First, we will put validatorDelegate :

  field . 

validatorDelegate = even

Then we implement a ] to update Visual Mode of our field when the Validation State is changed:

  func    validatorControl   (  validatorControl  :    ValidatorControl     changedValidState    validState :    Boolean )    {
    cover    la    view    = [19659043] validatorControl    as?    UIView    otherwise    {   return  } 

    if    validState    {
      display . [19659020] background color    =    UIColor .   white 
      error label .   hidden    =    true 
  }    otherwise    {
      display .   backg roundColor    =    UIColor .   red 
      error label .   hidden    =    false 
  } 
} 

And finally, we add a more function to show the actual reason why validation

failed (if failed):

  func    validatorControl   (  validatorControl :    ValidatorControl     violatedConditions    conditions :    [[196590101] Relationship ])    {
    var    errorText [19659010] =    "" 

    to    condition    under    conditions    {
      errorText    + =    condition .   localizedViolationString [19659075]} 

    errorLabel .   text    =    errorText 
    errorLabel .   hidden    =    false 
} 

Last but not sure not least, we can the fields in a given Form ask about everything is valid:

  var    form    =    ControlForm   () 

  form .   addEntry   (  field ) 
  form .  ] AddEntry  ]   anotherField ) [19659015]       isValid )    // true 

Learn more about FormValidatorSwift on git.io / formvalidator


Source link