ios - 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)
}
}
}
解决方案
当您在一行上添加 RuleRequired 时,在为该行设置 nil 值之后,并且由于 .ValidatesOnChange 选项,您会收到验证错误。你可以这样做:
@IBAction func resetButtonPressed(_ sender: UIBarButtonItem) {
form.removeAll()
createForm()
}
推荐阅读
- c# - Dotnet core 2.2 / IIS - AD 访问问题
- ignite - 为什么ignite的RAM不释放?
- python - 如何用户计划库每分钟运行一次功能
- javascript - 一个包的打字稿代码如何被其他包使用?
- video - FFMPEG - 在区域中滚动文本
- python - Pexpect:无法设置 shell 提示符
- google-cloud-platform - 使用 Deployment Manager 创建 Cloud SQL - “前提条件检查失败。” 错误
- algorithm - 如何获得两种不同标记化的对齐方式?(例如 BERT 与 spaCy)
- python - 有什么方法可以改变多类分类问题中目标类的数量?
- wordpress - 如何在 wordpress 的菜单项上添加通知徽章?