I have implemented alert window with two text fields. And I want to perform some validation: if one of text fields is blank (user did not input any value), application should not allow user to press the "Done" button. I don't want to show any alerts, just want to not allow user to save blank data.
I have created the function listed below. Added two guards with return. But in this case if user did not input anything, alert just closes and nothing is saved. I don't want alert window to be closed.
How can it be done if it can be? Have not found the answer. I have checked this question, but looks like it's not applicable for me. Appreciate your help!
private func addTable() {
let alert = UIAlertController(title: NSLocalizedString("inputTableParams", comment: ""), message: nil, preferredStyle: .alert)
alert.addTextField(configurationHandler: configureTableNameTextField)
alert.addTextField(configurationHandler: configureTableCapacityTextField)
alert.textFields?[0].autocapitalizationType = .sentences
alert.textFields?[1].autocapitalizationType = .sentences
alert.addAction(UIAlertAction(title: NSLocalizedString("alertCancel", comment: ""), style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .default, handler: { (UIAlertAction) in
guard self.tableNameTextField.text != "" else {return}
guard self.tableCapacityTextField.text != "" else {return}
let newTable = Table(tableName: self.tableNameTextField.text!, tableCapacity: Int(self.tableCapacityTextField.text!)!)
let result = try? TablesTable.getOrCreateTable(table: newTable)
if result != nil {
self.updateTableView()
}
}))
self.present(alert, animated: true, completion: {})
}