Skip to content

Instantly share code, notes, and snippets.

@bill350
Last active January 18, 2018 20:38
Show Gist options
  • Save bill350/b9b7de61a9844a5cec40b04495e680b0 to your computer and use it in GitHub Desktop.
Save bill350/b9b7de61a9844a5cec40b04495e680b0 to your computer and use it in GitHub Desktop.
import Foundation
class Form {
var formItems = [FormItem]()
var title: String?
var username: String?
var mail: String?
var phoneNumber: String?
init() {
self.configureItems()
self.title = "Amazing form"
}
// MARK: Form Validation
@discardableResult
func isValid() -> (Bool, String?) {
var isValid = true
for item in self.formItems {
item.checkValidity()
if !item.isValid {
isValid = false
}
}
return (isValid, nil)
}
/// Prepare all form Items ViewModels for the DirectStudioForm
private func configureItems() {
// Username
let usernameItem = FormItem(placeholder: "Enter your username")
usernameItem.uiProperties.cellType = FormItemCellType.textField
usernameItem.value = self.username
usernameItem.valueCompletion = { [weak self, weak usernameItem] value in
self?.username = value
usernameItem?.value = value
}
// Mail
let mailItem = FormItem(placeholder: "Enter you mail address")
mailItem.uiProperties.cellType = FormItemCellType.textField
mailItem.uiProperties.keyboardType = .emailAddress
mailItem.value = self.mail
mailItem.valueCompletion = { [weak self, weak mailItem] value in
self?.mail = value
mailItem?.value = value
}
// Phone Number
let phoneItem = FormItem(placeholder: "Enter your phone number")
phoneItem.uiProperties.cellType = FormItemCellType.textField
phoneItem.uiProperties.keyboardType = .phonePad
phoneItem.value = self.phoneNumber
phoneItem.valueCompletion = { [weak self, weak phoneItem] value in
self?.phoneNumber = value
phoneItem?.value = value
}
self.formItems = [usernameItem, mailItem, phoneItem]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment