logic for anyRequired
This commit is contained in:
parent
41656dccad
commit
227248afe0
@ -10,14 +10,40 @@ import UIKit
|
||||
|
||||
|
||||
public class RuleAnyRequiredModel: RulesProtocol {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
|
||||
public static var identifier: String = "anyRequired"
|
||||
public var type: String = RuleRequiredModel.identifier
|
||||
public var fields: [String]
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Methods
|
||||
//--------------------------------------------------
|
||||
|
||||
public func isValid(_ formField: FormFieldProtocol) -> Bool {
|
||||
guard let value = formField.formFieldValue() else { return false }
|
||||
|
||||
return true
|
||||
if let valueString = value as? String {
|
||||
return valueString.count > 0
|
||||
|
||||
} else if let valueBool = value as? Bool {
|
||||
return valueBool
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
public func isValid(_ formValidator: FormValidator) -> Bool {
|
||||
|
||||
for formKey in fields {
|
||||
guard let formField = formValidator.formField(for: formKey) else { continue }
|
||||
|
||||
if isValid(formField) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ public protocol RulesProtocol: ModelProtocol {
|
||||
var fields: [String] { get set }
|
||||
func isValid(_ formValidator: FormValidator) -> Bool
|
||||
func isValid(_ formField: FormFieldProtocol) -> Bool
|
||||
// func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool
|
||||
func setValid(_ formField: FormFieldProtocol, _ isValid: Bool)
|
||||
}
|
||||
|
||||
@ -36,22 +37,22 @@ public extension RulesProtocol {
|
||||
}
|
||||
|
||||
func isValid(_ formValidator: FormValidator) -> Bool {
|
||||
|
||||
var valid = true
|
||||
|
||||
for formKey in fields {
|
||||
guard let formField = formValidator.formField(for: formKey) else {
|
||||
continue
|
||||
}
|
||||
guard let formField = formValidator.formField(for: formKey) else { continue }
|
||||
let fieldValidity = isValid(formField)
|
||||
setValid(formField, fieldValidity)
|
||||
valid = valid && fieldValidity
|
||||
}
|
||||
|
||||
return valid
|
||||
}
|
||||
|
||||
func setValid(_ formField: FormFieldProtocol, _ isValid: Bool) {
|
||||
guard let formFieldValid = formField as? ValidProtocol else {
|
||||
return
|
||||
}
|
||||
guard let formFieldValid = formField as? ValidProtocol else { return }
|
||||
|
||||
formFieldValid.setValidity(isValid)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user