3

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.

1
  • Please mark an answer if it solved your question :)
    – Efren
    Dec 5, 2017 at 1:27

3 Answers 3

6

In the Eureka example project they use onRowValidationChanged and add/remove a LabelRow with a message that can be customised.

2

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

            }
}
1
  • Great suggestion, thanks! I hadn't thought about changing the titleLabel color yet! May 24, 2017 at 11:48
0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.