FxField
public class FxField<E:Hashable>: FxElement
Reactive class that manages the identity, value, and validation for a specific field or element on the form.
-
Enumeration or other hashable unique identifer for this field
Declaration
Swift
public let id: E
-
String representation of id.
Declaration
Swift
public var key: String
-
Field list that owns theis field.
Declaration
Swift
public weak var fields: FxFields<E>?
-
Construct Field with unique identifier. Construction usually occurs in
fields.add()
.fields.add(.zip)
Declaration
Swift
public init(_ id: E)
-
Defines the field title. Titles may be bound to labels and are also used for error message generation.
fields.add("zip").title("Zip Code")
Declaration
Swift
public func title(_ title: String?) -> FxField
Parameters
title
String or nil to clear value.
Return Value
Self for further configuration.
-
Undocumented
Declaration
Swift
public var title: String?
-
Adds the specified validation rule to the set of field validation rules.
fields.add("zip").validate(FxRules.zipcode)
Validation rules are only checked when field values are not empty. Use
.required()
to force validation to occur.Declaration
Swift
public func validate(_ rule: FxValidationRule) -> FxField
Parameters
rule
FxValidationRule.
Return Value
Self for further configuration.
-
Constructs and adds an unnamed validation rule to the set of field validation rules.
fields.add("zip").validate(errorZip) { $0.asText.count == 5 || $0.asText.count == 10 }
Validation fuctions are only checked when field values are not empty. Use
.required()
to force validation to occur.Declaration
Swift
public func validate(_ message: String? = nil, _ valid: @escaping FxFieldValidationFunction) -> FxField
Parameters
message
Optional custom error message for this rule.
valid
Validation function or closure. Returns true if value is valid.
Return Value
Self for further configuration.
-
Constructs and adds the specified validation rule to the set of field validation rules.
fields.add("zip").validate(name: "zip", message: errorZip) { $0.asText.count == 5 || $0.asText.count == 10 }
Validation fuctions are only checked when field values are not empty. Use
.required()
to force validation to occur.Declaration
Swift
public func validate(name: String, message: String? = nil, _ valid: @escaping FxFieldValidationFunction) -> FxField
Parameters
name
Rules with names allow resetting/changing rules.
message
Optional custom error message for this rule.
valid
Validation function or closure. Returns true if value is valid.
Return Value
Self for further configuration.
-
Returns true if required rule or validation rules exist.
Declaration
Swift
public var hasValidationRules: Bool
-
Removes all validation rules.
Declaration
Swift
public func removeAllValidationRules()
-
Removes the named rule from the list of validation rules.
Declaration
Swift
public func removeValidationRule(_ name: String)
-
Returns true if field is valid and all validation rules passed.
Note: Calling isValid will set or clear the field error message value based on validation success.
Declaration
Swift
public func isValid() -> Bool
-
Checks all validation rules. If validation fails error message will be returned, not set.
Declaration
Swift
public func checkValidation() -> String?
-
Validation triggers are used to fire validation rules when a given event occurs.
field.setValidationTrigger(field.rx.isChanged)
Declaration
Swift
public func setValidationTrigger(_ trigger: Observable<Bool>?)
Parameters
trigger
Observable that fires validation rules when next event is received.
-
Defines text formatter to be used for
rx.text
binding.fields.add(.state).format(FxTextFormatUppercased())
Declaration
Swift
public func format(_ formatter: FxTextFormatting?) -> FxField
Parameters
formatter
FxTextFormatting-based text formatter.
Return Value
Self for further configuration.
-
Gets and sets current formatter.
Declaration
Swift
public var formatter: FxTextFormatting?
-
Returns the field’s current value as formatted text.
Declaration
Swift
public var asFormattedText: String
-
Defines validation rule that states field text values must be at least N characters long.
fields.add("zip").min(length: 5)
Declaration
Swift
public func min(length: Int) -> FxField
Parameters
length
Minimum length for field value or 0 to reset.
Return Value
Self for further configuration.
-
Returns current maxLength value. Changing updates the validation rule appropriately.
Declaration
Swift
public var minLength: Int
-
Sets initial value of field. Value will be bound to view or control by FxForm.
fields.add(.amount).value(contact.amount) fields.add(.zip).value(contact.zip) fields.add(.message).value("This is a message.") fields.add(.agrees).value(false)
Declaration
Swift
public func value<T>(_ value: T?) -> FxField
Parameters
value
Value of type T.
Return Value
Self for further configuration.
-
Generic function returns current value as? type T. If T is unknown type returned value will be nil.
Declaration
Swift
public func value<T>() -> T?
-
Clears the current value.
Declaration
Swift
public func clear()
-
Returns true if current value is empty.
Declaration
Swift
public var isEmpty: Bool
-
Returns current value as Bool.
Declaration
Swift
public var asBool: Bool
-
Returns current value as Bool or nil if value is false.
Declaration
Swift
public var asOptionalBool: Bool?
-
Returns current value as Double.
Declaration
Swift
public var asDouble: Double
-
Returns current value as Double or nil if value is empty.
Declaration
Swift
public var asOptionalDouble: Double?
-
Returns current value as Int.
Declaration
Swift
public var asInt: Int
-
Returns current value as Int or nil if value is empty.
Declaration
Swift
public var asOptionalInt: Int?
-
Returns current value as String.
Declaration
Swift
public var asText: String
-
Returns current value as String or nil if value is empty.
Declaration
Swift
public var asOptionalText: String?
-
Gets the current error message. Value will be propagated to bound view, if any.
Declaration
Swift
public var error: String?
-
Undocumented
Declaration
Swift
public func clearError()
-
Defines the Enabled flag state for later binding to a control.
fields.add("zip").enabled(true)
Enabled status will not be automatically bound to view or control unless this method is called during the field definition phase prior to field binding.
Declaration
Swift
public func enabled(_ enabled: Bool) -> FxField
Parameters
enabled
Default is true.
Return Value
Self for further configuration.
-
Gets and sets the current enabled state. Value may be propagated to bound view.
Declaration
Swift
public var isEnabled: Bool
-
Defines the Hidden flag state for later binding to a view or control.
Note that hidden fields are not validated.
fields.add("zip").hidden(true)
Hidden status will not be automatically bound to view or control unless this method is called during the field definition phase prior to field binding.
Declaration
Swift
public func hidden(_ hidden: Bool) -> FxField
Parameters
hidden
Default is false.
Return Value
Self for further configuration.
-
Gets and sets the current hidden state. Value may be propagated to bound view.
Declaration
Swift
public var isHidden: Bool
-
Called from FxForm.bind() to perform standard value, error, and attribute bindings to the previously assigned view.
Note: Users should not need to call this function themselves.
Declaration
Swift
public func performBindings(_ exclusions: [FxFieldBindings] = [])
Parameters
exclusions
List of bindings already handled by user and that should not be performed.
-
Convenience method to bind our error message to a specfifc label.
bindErrorMessage(label)
Declaration
Swift
public func bindErrorMessage(_ label: UILabel?)
Parameters
label
UILabel to receive message.
Return Value
Self for additonal bindings or configuration.
-
Convenience method to bind our isEnabled flag to a specfifc control.
field.bindIsEnabled(.zipTextField)
Declaration
Swift
public func bindIsEnabled(_ view: UIView?)
Parameters
label
Control to receive flag.
Return Value
Self for additonal bindings or configuration.
-
Convenience method to bind our isHidden flag to a specfifc view.
field.bindIsHidden(.zipTextField)
Declaration
Swift
public func bindIsHidden(_ view: UIView?)
Parameters
id
Id of FxField to bind.
label
View to receive flag.
Return Value
Self for additonal bindings or configuration.
-
Convenience method to bind our title to a specfifc label.
form.autoBind() .bindTitle(.zip, zipLabel)
Declaration
Swift
public func bindTitle(_ label: UILabel?)
Parameters
id
Id of FxField to bind.
label
UILabel to receive title.
Return Value
Self for additonal bindings or configuration.
-
Convenience method to bind our value to a specfifc button title.
form.autoBind() .bindValue(.zip, myButton)
Declaration
Swift
public func bindValue(_ button: UIButton?)
Parameters
id
Id of FxField to bind.
label
UILabel to receive title.
Return Value
Self for additonal bindings or configuration.
-
Convenience method to bind our value to a specfifc label.
form.autoBind() .bindTitle(.zip, zipLabel)
Declaration
Swift
public func bindValue(_ label: UILabel?)
Parameters
id
Id of FxField to bind.
label
UILabel to receive title.
Return Value
Self for additonal bindings or configuration.
-
Defines validation rule that states field text values may not be more than N characters long.
fields.add("zip").max(length: 5)
Declaration
Swift
public func max(length: Int) -> FxField
Parameters
length
Max length for field value or 0 to reset.
Return Value
Self for further configuration.
-
Returns current maxLength value. Changing updates the validation rule appropriately.
Declaration
Swift
public var maxLength: Int
-
Defines validation rule that the field value is optional. Will clear the Required validation rule.
fields.add("zip").optional()
Declaration
Swift
public func optional() -> FxField
Return Value
Self for further configuration.
-
Defines validation rule that indicates value must not be nil or empty. Will clear the Optional validation rule.
fields.add("state").required(length: 2) fields.add("zip").required()
Declaration
Swift
public func required(length: Int = 0) -> FxField
Parameters
length
If specifed field must be exactly the length indicated.
Return Value
Self for further configuration.
-
Undocumented
Declaration
Swift
public var isRequired: Bool
-
Defines the placeholder value that will be passed to the bound textfield.
fields.add("zip").placeholder("00000-0000")
This value will not be automatically bound to view or control unless this method is called during the field definition phase prior to field binding.
Declaration
Swift
public func placeholder(_ string: String?) -> FxField
Parameters
string
String.
Return Value
Self for further configuration.
-
Undocumented
Declaration
Swift
public var placeholder: String?
-
This observable automatically fires whenever the base value is set or updated
Declaration
Swift
public func asObservable() -> Observable<FxField<E>>
-
Defines the error message that should be returned if a validation rule fails.
fields.add("zip").required().message("Zip code is required")
Note that this message overrides any individual validation rule messages.
Declaration
Swift
public func message(_ message: String?) -> FxField
Return Value
Self for further configuration.
-
Gets and sets current message value. Setting may propagate value to bound views.
Declaration
Swift
public var message: String?
-
Defines keyboard type for later binding to textfield.
fields.add("zip").keyboard(.numberpad)
Note value will not be automatically bound to view or control unless this method is called during the field definition phase prior to field binding.
Declaration
Swift
public func keyboardType(_ type: UIKeyboardType?) -> FxField
Return Value
Self for further configuration.
-
Gets and sets current keyboard value. Setting may propagate value to bound views.
Declaration
Swift
public var keyboardType: UIKeyboardType?