XLForm在Swift中的验证



我正在用这个简单的表单处理XLForm。代码是用Swift编写的。我在验证方面有一个问题——我想使用XLForm的内部验证器来处理电子邮件和其他字段,但我不知道如何处理。我只需要检查其他字段是否填写了数据。手册是用Obj-C编写的,我在Swift中找不到任何例子。有人能给我一些如何实现它的提示吗?我尝试使用userEmail.required=true,但它不起作用。我一直在寻找一些方法来实现saveTapped方法,在发送表单之前验证字段,但我找不到任何解决方案。

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
}

}

    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
    }

将此代码放入保存的Tapped

最新更新