iOS-重置表单时显示Eureka验证错误

时间:2018-08-26 04:40:22

标签: ios swift validation reset eureka-forms

我收到一条错误验证消息。当我按下表单的重置按钮时就会显示出来。我认为是因为.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)
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

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

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