简体   繁体   中英

Swift 3 Eureka Validation UILabels with Errors

I've just started using Eureka Form Builder for Swift 3 but I'm wondering if there is a way to show validation errors. I've added my form with one row in it below where I'm trying to set the error message to an optional detailTextLabel, however it's not showing. My question is, does Eureka have a default error message UILabel built into the rows or do I have to make a custom row that is able to show the message?

form +++ Section("Contactgegevens")
        <<< NameRow(){ row in
            row.title = "Achternaam"
            row.placeholder = "Achternaam"
            row.value = user?.surname
            row.add(rule: RuleRequired(msg: "Dit veld is verplicht."))
            row.validationOptions = .validatesOnChangeAfterBlurred
            }.onChange({ (row) in
                if !row.isValid {

                    var errors = ""

                    for error in row.validationErrors {
                        let errorString = error.msg + "\n"
                        errors = errors + errorString
                    }
                    print(errors)
                    row.cell.detailTextLabel?.text = errors
                    row.cell.detailTextLabel?.isHidden = false

                }
            })

The documentation on Eureka seems to be really incomplete, but the framework itself does seem really robust so I'd like to learn how to use it.

在Eureka 示例项目中,他们使用onRowValidationChanged并添加/删除LabelRow ,其中包含可自定义的消息。

It looks like you'd want to use .cellUpdate rather than .onChange . Another option for pointing out errors would be to change the titleLable.textColor to .red . If you do need to display the specific error message you could do it the way you are trying, just know that the detailTextLable can overlap your value. I'd suggest maybe a left alignment and short error messages if you go this route. Also good to note that if you'd like the errors to be populated as the user is typing you'd want to use .validatesOnChange rather than .validatesOnChangeAfterBlurred . I'm not sure if Eureka has changed their documentation but most of this info can be found in the supporting documentation @ https://github.com/xmartlabs/Eureka

form +++ Section("Contactgegevens")
    <<< NameRow(){ row in
        row.title = "Achternaam"
        row.placeholder = "Achternaam"
        row.value = user?.surname
        row.add(rule: RuleRequired(msg: "Dit veld is verplicht."))
        row.validationOptions = .validatesOnChangeAfterBlurred
        }.cellUpdate { cell, row in
            if !row.isValid {

                cell.titleLabel?.textColor = .red

                var errors = ""

                for error in row.validationErrors {
                    let errorString = error.msg + "\n"
                    errors = errors + errorString
                }
                print(errors)
                cell.detailTextLabel?.text = errors
                cell.detailTextLabel?.isHidden = false
                cell.detailTextLabel?.textAlignment = .left

            }
}

I was able to change the validation text style by inserting a new label into the cells subview. hope this helps someone else as frustrated as I was

             if !row.isValid {
                for (index, validationMsg) in row.validationErrors.map({ $0.msg }).enumerated() {
                    let labelRow = LabelRow() {
                    let errorLabel = UILabel()
                        errorLabel.text = validationMsg
                        errorLabel.frame = CGRect(x:0, y:0, width:$0.cell.frame.width, height:30)
                        errorLabel.textColor = UIColor.red
                        errorLabel.textAlignment = .left
                        $0.cell.addSubview(errorLabel)
                    }
                    row.section?.insert(labelRow, at: row.indexPath!.row + index + 1)
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM