1

我正在使用这个简单的表单使用 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
}

}

4

1 回答 1

1
    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
    }

将此代码放入您的 saveTapped

于 2015-04-25T02:12:37.610 回答