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

标签 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)
                    }
                }
            }

最佳答案

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

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

关于iOS - 重置表单时显示 Eureka 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023243/

相关文章:

iphone - Objective c - 使用一个或多个 ViewController 在 View 之间翻转?

ios - 如何将 UIimage View 旋转到触摸屏幕的位置?

ios - 如何快速显示来自 UIPreviewAction 的 View Controller

php - 如何使用 PHP 验证正则表达式

javascript - 表单验证 - 如何跟踪多个字段以防止提交 - 全局变量?

php - 如何在 iOS 中将 base64_encode 从 PHP 重写为 Swift 3?

ios - 为了使模糊的 UIVibrancyEffect 起作用,我们是否必须将它作为单独的 View 添加到 UIBlurEffect contentView?

ios - 编程自定义单元格内存泄漏问题

swift - 无法将类型 'Result<Data, Error>' 的值转换为闭包结果类型 'Result<T, E>'

django - 何时以及如何使用 Django REST 框架验证数据