Get form values from multivalued sections of an Eureka form
Ask this question because it is not yet covered in the docs and they track and respond to this tag. I am using Eureka to create 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 the NameRows at the end. There can be any number of lines (based on user input). This is what I tried:
self.form.values()
but this results in [ : ]
.
How do I get all the values?
source to share
For those with a similar problem:
form.values()
empty because there were no labels for the lines. To get values from form
, give tags to strings and you will get a dictionary of values with keys like these tags. Ad hoc
+++ 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"
}
}
Values will now be returned as ["tag_1" : "value 1", "tag 2 : "value 2" ....]
for any number of rows inserted by the user.
PS: Used tag index
within tag because duplicate tags are not allowed and the meaning index
is different for different strings.
source to share
Form.value () returns a Dictionary and the dictionary has no ordering information.
Thus, you cannot get overridden values from form.values ().
I got the orderd data like below:
(form.allRows returns Array and has order information.)
// Dictionary doen't have order info.
let values: Dictionary = form.values()
NSLog("LogA : %@", values)
// Array has order info, and this can return all tags.
let allRow: Array<BaseRow> = form.allRows
for i in 0..<allRow.count {
let tmp: BaseRow = allRow[i]
NSLog("LogB : %d : %@", i, tmp.tag ?? "")
}
// So make ordered data by from.values() and form.allRows like this.
var results: Array = [String]()
for i in 0..<allRow.count {
let tag: String = allRow[i].tag ?? ""
if(tag != ""){
let val: String = values[tag] as! String
results.append(tag + ":" + val)
}
}
NSLog("LogC = %@", results)
thank
source to share
It's much easier
First of all, add a tag to the entire MultivaluedSection object.
+++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
header: "Options",
footer: "footer") {
$0.tag = "someTag" // added this line
$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"
}
}
and then you can get the values by
let values = (self.form.values()["someTag"]!! as! [Any?]).compactMap { $0 }
source to share