Reputation: 9
I have a Form (tableview with questions and textfields). It is not static as data may change depending upon the form user select. So I store these questions and validation constraints in a dictionary. How can I validate these fields before user press submit ? Code is given below :
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell:FormTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cellformid") as! FormTableViewCell
let dict = questions?[indexPath.row]
Cell.form_question_label.text = dict?.question_name
Cell.form_answer_textfield.text = dict?.answer ?? ""
return Cell
}
//Submit Button Action
@IBAction func saveFormButtonPressed(_ sender: Any) {
//Here I do operations to save a Array which contain all attended answers.
}
NOTE : When I press submit button data entered by user is being saved to database, but Validation cannot be done,because if some question is unattended that is not stored in database and also is not attached to the Array. So I cannot check whether all Mandatory fields are completed.
Please suggest a method..
Upvotes: 1
Views: 464
Reputation: 1464
Try this.
for view in self.view.subviews {
if (view is UITextField) {
var textfield = view as! UITextField
if (textfield.text == "") {
// execute code
}
}
}
Upvotes: 0
Reputation: 718
You can add to your UITextField
:
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
and then validate answers:
@objc func textFieldDidChange(_ textField: UITextField) {
//Validation
}
Upvotes: 1