Swift: How to get form values using Eureka form builder?
I figured it out myself. It wasn't immediately clear to me that you need to set tags on the rows you want to retrieve the value from:
<<< NameRow() {
$0.tag = "NameRow"
$0.title = "Name:"
$0.value = "My name"
}
let dict = form.values(includeHidden: true)
print(dict["NameRow"]) // Prints my name
Get values from Eureka Forms in Swift
You need to use the .onChange
to update your values once DateRow or TextRow changes, or you can access directly to the value with form.rowBy(tag: "tagName")
and casting to the correct type of row you can access to .value
in this example code using your base code I use both approaches
import UIKit
import Eureka
class GettingDataViewController: FormViewController {
//Creating Global Variables
var name: String = ""
var data: Date? = nil
@IBAction func testebutton(_ sender: Any)
{
print(name)
print(data)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
form +++ Section()
<<< TextRow()
{
row in
row.title = "Name"
row.tag = "name"
}.onChange({ (row) in
self.name = row.value != nil ? row.value! : "" //updating the value on change
})
<<< DateRow()
{
$0.title = "Birthdate"
$0.value = Date()
$0.tag = "date"
}.onChange({ (row) in
self.data = row.value //updating the value on change
})
<<< ButtonRow(tag: "test").onCellSelection({ (cell, row) in
print(self.name)
print(self.data)
//Direct access to value
if let textRow = self.form.rowBy(tag: "name") as? TextRow
{
print(textRow.value)
}
if let dateRow = self.form.rowBy(tag: "date") as? DateRow
{
print(dateRow.value)
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift - Get value of textField form with Eureka
As vadian suggested You can detect text change with onChange
method as shown below:
.onChange({ decimal in
print(decimal)
})
And final code will be:
{
$0.header?.height = { 20 }
}
<<< DecimalRow(){
$0.title = " "
$0.tag = "MyRowTag"
}
.onChange({ decimal in
print(decimal)
})
.cellUpdate { cell, row in
cell.textField?.font = .boldSystemFont(ofSize: 18.0)
}
Get Form Values from Multivalued sections of Eureka Form
For anyone with similar problem:
The form.values()
was being 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.
Hidden fields not submitting their values in Eureka swift form
You can supply a includeHidden
argument to the values
method to get all the values including hidden ones as well:
let values = form.values(includeHidden: true)
How can I get MultivaluedSection values from Eureka?
I always did it like this:
let values: [String]? = (form.sectionBy(tag: "instructions")?.flatMap { ($0 as? ActionSheetRow<String>)?.value })
Basically,
- get the section with the tag
- try to convert each row into a
ActionSheetRow<String>
- map each row that can be converted to its value.
Related Topics
Redeclaring Members in an Extension Hides the Original Member *Sometimes*. Why
Add Constraints to Generic Parameters in Extension
Suppressing Implicit Returns in Swift
Swift: When Should I Use "Var" Instead of "Let"
Executing Text-To-Speech in Order
Firebase Uid VS Document-Id and Firestore Rules
How to Get Indexpath.Row in Cell.Swift
Codable Class Does Not Conform to Protocol Decodable
Nsmanagedobject Changes Do Not Trigger Objectwillchange
Declaring and Using Custom Attributes in Swift
If the Swift 'Guard' Statement Must Exit Scope, What Is the Definition of Scope
Gcd with Static Functions of a Struct
Unable to Save On/Off State of a Uitableviewcell