XLForm validation in Swift

1.2k Views Asked by At

I'm working with XLForm with this simple form. Code is written in Swift. I have a problem with validation - I would like to use XLForm's internal validator for email and for other fields, but I don't know how. I just need to check if other fields are filled with data. Manual is written in Obj-C and I could not find any examples in Swift. Could anyone pass me some hints how to implement it? I was trying with userEmail.required = true but it's not working. I was looking for some method to implement in saveTapped method, to validate fields before I will send the form, but I was not able to find any solution.

class FormViewController: XLFormViewController {


required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder);
    self.setupForm()
}

override func viewDidLoad() {
    super.viewDidLoad()
}
@IBAction func saveTapped(sender: AnyObject) {


            println(form.formRowWithTag("userEmail").value as? String)
            println(form.formRowWithTag("userPassword").value as? String)
            println(form.formRowWithTag("userName").value as? String)

}

private func setupForm() {

    let form = XLFormDescriptor(title: "Registration")

    // Section 1
    let section1 = XLFormSectionDescriptor.formSection() as XLFormSectionDescriptor
    form.addFormSection(section1)

    let userEmail = XLFormRowDescriptor(tag: "userEmail", rowType: XLFormRowDescriptorTypeText, title: "Email")
    userEmail.required = true
    section1.addFormRow(userEmail)

    let userPassword = XLFormRowDescriptor(tag: "userPassword", rowType: XLFormRowDescriptorTypePassword, title: "Password")
    userPassword.required = true
    section1.addFormRow(userPassword)


    let userName = XLFormRowDescriptor(tag: "userName", rowType: XLFormRowDescriptorTypePassword, title: "First name")
    userName.required = true
    section1.addFormRow(userName)


    self.form = form
}

}

1

There are 1 best solutions below

0
On
    let validationErrors:NSArray = self.formValidationErrors()
    if (validationErrors.count > 0) {
        var errorString = ""
        for error in validationErrors {
            errorString += error.localizedDescription + "\n"
        }
        UIAlertView(title: "Error! Please check again.", message: errorString, delegate: nil, cancelButtonTitle: "OK").show()
        return false
    }

put this code into your saveTapped