iosswifteureka-forms

Get Form Values from Multivalued sections of Eureka Form


Asking this question here because this has not been covered in docs yet and they monitor and answer this tag. I am using Eureka to build a Multivalued Form. This is my code:

    +++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
        header: "Options",
        footer: "footer") {
                            $0.addButtonProvider = { section in
                                return ButtonRow(){
                                    $0.title = "Add New Option"
                                }
                            }
                            $0.multivaluedRowToInsertAt = { index in
                                print(self.form.values())
                                return NameRow() {
                                    $0.placeholder = "Your option"
                                }
                            }
                            $0 <<< NameRow() {
                                $0.placeholder = "Your option"
                            }
    }

Now I want to extract all the values in NameRows at the end. There can be any number of rows(based on user input). This is what I tried:

self.form.values() but it results in [ : ].

How do I get all the values?


Solution

  • For anyone with similar problem:

    The form.values() was empty because there were no tags given to the Rows. To get values from form, give tags to rows and you will get a dictionary of values with keys as these tags. For this case

    +++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
                               header: "header",
                               footer: "footer") {
                                $0.addButtonProvider = { section in
                                    return ButtonRow(){
                                        $0.title = "Button Title"
                                    }
                                }
                                $0.multivaluedRowToInsertAt = { index in
                                    return NameRow("tag_\(index+1)") {
                                        $0.placeholder = "Your option"
                                    }
                                }
                                $0 <<< NameRow("tag_1") {
                                    $0.placeholder = "Your option"
                                }
        }
    

    Now the values will be returned as ["tag_1" : "value 1", "tag 2 : "value 2" ....] for any number of rows inserted by user.
    P.S.: Used the index in tag because duplicate tags aren't allowed and index value is different for different rows.