Search code examples
iosswiftswift3eureka-forms

Eureka Forms Swift enable and disable button


I'm using Eureka Forms to make a Register functions in my app.

And I would like to figure out this issue.

The user has some fields that is required to give us. Until he rights down all the info and he check all the buttons that we want, I would like the button submit to be Disabled or Hidden. And when the rows that are required, are completed, then the button should be available / shown.

This is the Class that i've made with eureka

class RegisterViewController: FormViewController {

    var xwra = 1
    override func viewDidLoad() {
        super.viewDidLoad()

        LabelRow.defaultCellUpdate = { cell, row in
            cell.contentView.backgroundColor = .red
            cell.textLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 13)
            cell.textLabel?.textAlignment = .right

        }

        TextRow.defaultCellUpdate = { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .red
            }
        }
        PhoneRow.defaultCellUpdate = { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .red
            }
        }
        PickerInlineRow<String>.defaultCellUpdate = { cell, row in
            if !row.isValid {
                cell.backgroundColor = .red
            }
        }

        PasswordRow.defaultCellUpdate = { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .red
            }
        }
        ButtonRow.defaultCellUpdate = { cell, row in
            cell.textLabel?.numberOfLines = 0
        }

        form +++ Section("Τα Προσωπικά Στοιχεία σας")
            <<< TextRow("name"){ row in
                row.title = "Όνομα"
                row.add(rule: RuleRequired())
                row.validationOptions = .validatesOnChange
                row.placeholder = "Enter text here"
                }
                .cellUpdate { cell, row in
                    if !row.isValid {
                        cell.titleLabel?.textColor = .red
                    }
            }
            <<< TextRow("surname"){ row in
                row.title = "Επώνυμο"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
            }
            <<< TextRow("email"){ row in
                row.title = "Email"
                row.add(rule: RuleRequired())
                row.add(rule: RuleEmail())
                row.placeholder = "Enter text here"
            }
            <<< PhoneRow("phone"){

                $0.title = "Τηλέφωνο"
                $0.add(rule: RuleRequired())
                $0.placeholder = "Enter text here"
            }
            <<< PhoneRow("fax"){
                $0.title = "Φαξ"
                $0.placeholder = "And numbers here"
            }
            +++ Section("Η Διεύθυνση σας")
            <<< TextRow("company"){ row in
                row.title = "Επωνυμία"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< TextRow("job"){ row in
                row.title = "Επάγγελμα"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< TextRow("afm"){ row in
                row.title = "Α.Φ.Μ. / Δ.Ο.Υ"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< TextRow("address1"){ row in
                row.title = "Διεύθυνση 1"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< TextRow("address2"){ row in
                row.title = "Διεύθυνση 2"
                row.placeholder = "Enter text here"
        }
            <<< TextRow("town"){ row in
                row.title = "Πόλη"
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< TextRow("tk"){ row in
                row.title = "Τ.Κ."
                row.add(rule: RuleRequired())
                row.placeholder = "Enter text here"
        }
            <<< PickerInlineRow<String>("countrytag") {
                $0.title = "Χώρα"
                $0.options = ["Ελλάδα", "Κύπρος"]
                $0.add(rule: RuleRequired())
                }.onChange { row in
                    if row.value == "Ελλάδα" {
                        self.xwra = 1
                    } else {
                        self.xwra = 0
                    }
            }
            <<< PickerInlineRow<String>("loctag") {
                $0.title = "Πόλη"
                $0.add(rule: RuleRequired())
                }.onCellSelection({ (cell, row) in
                    if self.xwra == 1 {
                        print("its one")
                        row.options = ["Ελλάδα", "Κύπρος", "Κύπρος", "Κύπρος", "Κύπρος"]
                        row.updateCell()
                    } else {
                        print("its not one")
                        row.options = ["Ελλάδα"]
                        row.updateCell()
                    }
                })
            +++ Section(header: "O Κωδικος σας", footer: "Θα πρέπει να είναι ίδιος και στα δύο πεδία.")

            <<< PasswordRow("password") {
                $0.title = "Password"
            }
            <<< PasswordRow() {
                $0.title = "Confirm Password"
                    $0.add(rule: RuleEqualsToRow(form: form, tag: "password"))
                }
                .cellUpdate { cell, row in
                    if !row.isValid {
                        cell.titleLabel?.textColor = .red
                    }
            }

            +++ Section("Λήψη Ενημερωτικών Δελτίων")
            <<< SwitchRow("SwitchRow") { row in
                row.title = "The title"
                    }

            +++ SelectableSection<ImageCheckRow<String>>("", selectionType: .singleSelection(enableDeselection: true))
            <<< ImageCheckRow<String>(){ lrow in
                lrow.title = "Έχω διαβάσει και αποδέχομαι τους όρους χρήσης - δήλωσης απορρήτου"
                lrow.selectableValue = ""
                lrow.value = nil
                }.cellSetup { cell, _ in
                    cell.trueImage = UIImage(named: "selectedRectangle")!
                    cell.falseImage = UIImage(named: "unselectedRectangle")!

            }

            <<< ButtonRow("terms") {
                $0.title = "Όροι Χρήσης - Δήλωση Απορρήτου"
                $0.presentationMode = .segueName(segueName: "oroi", onDismiss: nil)
            }

            <<< ButtonRow("register") {
                $0.title = "Εγγραφή"
                $0.cell.backgroundColor = .red
                $0.cell.tintColor = .white
                }
                .onCellSelection { cell, row in
                    //send data to server function

        }
    }

  }

Could someone point me of how to make it work? Thanks a lot!

Update 1.

The last button

<<< ButtonRow("register") {
                $0.title = "Εγγραφή"
                $0.cell.backgroundColor = .red
                $0.cell.tintColor = .white
                }
                .onCellSelection { cell, row in
                    row.section?.form?.validate()

        }

On cell selection, check all the data that are required and if they are not OK the text goes red. So the question is again the same. How do i run the send to server function in case validation is true?


Solution

  • I had the same problem, the following code get the errors count if it is 0 the form is OK and proceeds to your next function if it has any errors it does nothing.

    <<< ButtonRow() { (row: ButtonRow) -> Void in
                row.title = "LOGIN"
                }
                .onCellSelection { [weak self] (cell, row) in
                    print("validating errors: \(row.section?.form?.validate().count)")
                    if row.section?.form?.validate().count == 0{
                        self?.loginAction(row)
                    }
        }