首页 > 解决方案 > iOS - 当我重置表单时显示 Eureka 验证错误

问题描述

我收到一条错误验证消息。当我按下表单的重置按钮时,就会出现这种情况。我认为这是因为.validatesOnChange

但是,我不想把它拿走,因为当我尝试提交表单时验证本身可以正常工作。如果未输入任何内容,则会弹出消息,如果用户输入内容,则消息会消失。

重置表单时如何保持验证功能而不触发它?有没有更好的方法来重置我的表单?还是让我的验证消息出现和消失的更好方法?

@IBAction func resetButtonPressed(_ sender: UIBarButtonItem) {
    form.setValues(["bmi": nil])
    tableView.reloadData()
}

func createForm(){

    LabelRow.defaultCellUpdate = { cell, row in
        cell.contentView.backgroundColor = .red
        cell.textLabel?.textColor = .white
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        cell.textLabel?.textAlignment = .right
    }

    form +++ Section("Demographics")

        <<< DecimalRow("bmi") {
            $0.title = "BMI"
            $0.placeholder = "40"
            $0.useFormatterDuringInput = true
            $0.add(rule: RuleRequired())
            $0.validationOptions = .validatesOnChange
            }
            .onRowValidationChanged { cell, row in
                let rowIndex = row.indexPath!.row
                while row.section!.count > rowIndex + 1 && row.section?[rowIndex  + 1] is LabelRow {
                    row.section?.remove(at: rowIndex + 1)
                }
                if !row.isValid {
                    for (index, validationMsg) in row.validationErrors.map({ $0.msg }).enumerated() {
                        let labelRow = LabelRow() {
                            $0.title = validationMsg
                            $0.cell.height = { 30 }
                        }
                        row.section?.insert(labelRow, at: row.indexPath!.row + index + 1)
                    }
                }
            }

标签: iosswiftvalidationreseteureka-forms

解决方案


当您在一行上添加 RuleRequired 时,在为该行设置 nil 值之后,并且由于 .ValidatesOnChange 选项,您会收到验证错误。你可以这样做:

@IBAction func resetButtonPressed(_ sender: UIBarButtonItem) {
    form.removeAll()
    createForm()
}

推荐阅读