Search code examples
iosswifteureka-forms

Required field in Eureka form


I want to do required field if row percentageField or valueField isn't nil.

KalkulatinTag is enum:

enum KalkulationTag: Int {
    case skonto = 11
    case tage= 22
}

Hese is code in KalculationCell ViewController:

if (self.tag == KalkulationTag.skonto.rawValue) {
    if ((percentageField.text != nil) || (valueField.text != nil)) {
        if (self.tag == KalkulationTag.skonto.rawValue){
            //here should be required field of eureka
        }
    }
}

and here is the code that calls eureka forms: KalkulationRow is Costum row and IntRow is Eureka row. I want IntRow() value to be required.

form +++ Section("SKONTO")
    <<< IntRow() {
            $0.title = "Tage:"
            $0.cell.tag = KalkulationTag.tage.rawValue
        }
    <<< KalkulationRow { row in
            row.cell.tag = KalkulationTag.skonto.rawValue
        }

Project is here:

enter image description here


Solution

  • According to the validation docs at Eureka, required row can be created by adding RuleRequired rule to the row. Also, there is function that allows you to remove any rule you want.

    So, the strategy should be like this:

    1) On initial row setup check the percentageField and valueField values. If any of those equal to nil or empty - add RuleRequired to the row.

    2) Next step you should manage onChanged closure. Again, for percentageField and valueField check in onChanged closure, that rows has a value not a nil or empty. If row has values - remove required rule, if has - add.