如何验证自定义 Eureka 行中的文本字段等对象?
How can I validate an object like textfield in a custom Eureka row?
我已经使用 Eureka 框架创建了一个简单的自定义行,但是验证不起作用,我如何验证该行内的文本字段?我研究了 Google 但没有任何帮助。谁能帮帮我?
public class TitleAndTextFieldCellModel: NSObject {
// MARK: - Variable
public var title: String = ""
public var placeHolder: String = ""
public var inputValue: String = ""
public var object: Any?
// MARK: - Init
public init(title: String, placeHolder: String = "", inputValue: String = "", object: Any? = nil) {
self.title = title
self.placeHolder = placeHolder
self.inputValue = inputValue
self.object = object
}
}
public class TitleAndTextFieldCell: Cell<TitleAndTextFieldCellModel>, CellType {
// MARK: - Outlets
@IBOutlet weak public var titleLabel: UILabel!
@IBOutlet weak public var inputTextField: UITextField!
override public func setup() {
super.setup()
selectionStyle = .none
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
}
override public func update() {
super.update()
if let model = row.value {
titleLabel.text = model.title
inputTextField.placeholder = model.placeHolder
inputTextField.text = model.inputValue
}
}
@objc func textFieldDidChange(_ textField: UITextField) {
self.row.value?.inputValue = textField.text ?? ""
}
}
final public class TitleAndTextFieldRow: Row<TitleAndTextFieldCell>, RowType {
required public init(tag: String?) {
super.init(tag: tag)
cellProvider = CellProvider<TitleAndTextFieldCell>(nibName: "TitleAndTextFieldCell")
}
}
我是这样使用的:
<<< TitleAndTextFieldRow() {
[=11=].value = TitleAndTextFieldCellModel(title: "Name")
[=11=].add(rule: RuleRequired(msg: "Field required"))
[=11=].validationOptions = .validatesOnChangeAfterBlurred
}.cellUpdate({ (cell, row) in
if !row.isValid {
cell.inputTextField.layer.borderWidth = 1
cell.inputTextField.layer.borderColor = UIColor.red.cgColor
}
})
我想我自己找到了答案。对于那些需要的人,请执行以下代码:
@objc func textFieldDidChange(_ textField: UITextField) {
if textField.text == "" {
self.row.validationErrors.append(ValidationError(msg: "Required"))
self.row.validate()
}
self.row.value?.inputValue = textField.text ?? ""
}
我已经使用 Eureka 框架创建了一个简单的自定义行,但是验证不起作用,我如何验证该行内的文本字段?我研究了 Google 但没有任何帮助。谁能帮帮我?
public class TitleAndTextFieldCellModel: NSObject {
// MARK: - Variable
public var title: String = ""
public var placeHolder: String = ""
public var inputValue: String = ""
public var object: Any?
// MARK: - Init
public init(title: String, placeHolder: String = "", inputValue: String = "", object: Any? = nil) {
self.title = title
self.placeHolder = placeHolder
self.inputValue = inputValue
self.object = object
}
}
public class TitleAndTextFieldCell: Cell<TitleAndTextFieldCellModel>, CellType {
// MARK: - Outlets
@IBOutlet weak public var titleLabel: UILabel!
@IBOutlet weak public var inputTextField: UITextField!
override public func setup() {
super.setup()
selectionStyle = .none
inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
}
override public func update() {
super.update()
if let model = row.value {
titleLabel.text = model.title
inputTextField.placeholder = model.placeHolder
inputTextField.text = model.inputValue
}
}
@objc func textFieldDidChange(_ textField: UITextField) {
self.row.value?.inputValue = textField.text ?? ""
}
}
final public class TitleAndTextFieldRow: Row<TitleAndTextFieldCell>, RowType {
required public init(tag: String?) {
super.init(tag: tag)
cellProvider = CellProvider<TitleAndTextFieldCell>(nibName: "TitleAndTextFieldCell")
}
}
我是这样使用的:
<<< TitleAndTextFieldRow() {
[=11=].value = TitleAndTextFieldCellModel(title: "Name")
[=11=].add(rule: RuleRequired(msg: "Field required"))
[=11=].validationOptions = .validatesOnChangeAfterBlurred
}.cellUpdate({ (cell, row) in
if !row.isValid {
cell.inputTextField.layer.borderWidth = 1
cell.inputTextField.layer.borderColor = UIColor.red.cgColor
}
})
我想我自己找到了答案。对于那些需要的人,请执行以下代码:
@objc func textFieldDidChange(_ textField: UITextField) {
if textField.text == "" {
self.row.validationErrors.append(ValidationError(msg: "Required"))
self.row.validate()
}
self.row.value?.inputValue = textField.text ?? ""
}