[![CI Status](https://img.shields.io/travis/Ahmed Tarek/FormGenerator.svg?style=flat)](https://travis-ci.org/Ahmed Tarek/FormGenerator)
To run the example project, clone the repo, and run pod install
from the Example directory first.
FormGenerator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Vanguard', :git => 'https://bitbucket.org/baianat/vanguard-ios.git'
pod 'FormGenerator', :git => 'https://bitbucket.org/baianat/formgenerator-ios.git'
First things first, you describe your form in a custom class you create using descriptors provided by FormGenerator
import UIKit
import FormGenerator
class RegisterInput {
var dualName = HorizontalDualFieldDescriptor(
first: NameFieldDescriptor(label: "First Name"),
second: NameFieldDescriptor(label: "Last Name")
)
var email = EmailFieldDescriptor(label: "Email")
var phone = PhoneFieldDescriptor(label: "Phone")
var city = PickerFieldDescriptor.createWithAlertPicker(alertTitle: "Choose City", label: "City")
var password = PasswordFieldDescriptor(
shouldUsePasswordMeter: true,
label: "Password"
)
var bio = ParagraphDescriptor(label: "Bio", height: .flexible(minHeight: 54, maxHeight: 120))
var privacyAndTerms = ConsentCheckBoxDescriptor(decorator: .dummy, defaultValue: false)
}
public extension ConsentDecorator {
static let dummy: ConsentDecorator = ConsentDecorator(
textSlices: [
NormalTextSlice(text: "I accept"),
SpaceTextSlice(),
LinkTextSlice(text: "Terms of use", link: "https://google.com"),
SpaceTextSlice(),
NormalTextSlice(text: "and"),
SpaceTextSlice(),
LinkTextSlice(text: "Privacy Policy", link: "1"),
NormalTextSlice(text: ".")
],
textColor: .blue,
checkBoxFillColor: .green,
checkBoxCheckColor: .red)
}
Then you instantiate both your class and FormGenerator in your ViewController and just call
generate(forInput: input, inContainer: containerView)
you can provide UIStackView, UIView or UIScrollView
import UIKit
import FormGenerator
class ViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
var form = FormGenerator()
var input = RegisterInput()
override func viewDidLoad() {
super.viewDidLoad() {
input.city.values = ["Port Said", "Damietta", "Cairo"]
form.applyStyle(
style: FormStyle(
textFieldFont: UIFont.systemFont(ofSize: 16),
inactiveBorderColor: .gray,
activeBorderColor: .blue,
textColor: .black
)
)
form.generate(forInput: input, inContainer: containerView)
}
@IBAction func buttonAction(_ sender: Any) {
form.evaluateInput()
}
}
You can validate the form and fill input by calling
form.evaluateInput()
You can access the entered and selected values from the descriptors in the class you created
print("First Name: \(input.dualName.first.selectedValue)")
print("Last Name: \(input.dualName.second.selectedValue)")
print("Email: \(input.email.selectedValue)")
print("Phone: \(input.phone.selectedValue?.fullPhoneNumber)")
print("City: \(input.city.selectedValue)")
print("Password: \(input.password.selectedValue)")
print("PrivacyAndTerms: \(input.privacyAndTerms.selectedValue)")
Purpose: refers to input field for name Params: label -> for field title, rules -> for field validation (Rules are provided by Vanguard)
Purpose: refers to input field for email Params: label -> for field title, rules -> for field validation (Rules are provided by Vanguard)
Purpose: refers to input field for phone numbers
Params: defaultCountryCode -> in iso format ex: "eg" label -> for field title, rules -> for field validation (Rules are provided by Vanguard)
Purpose: refers to input field for picker Creators: Using AlertPicker
PickerFieldDescriptor.createWithAlertPicker(
alertTitle: String? = nil,
label: String? = nil,
rules: [Rule] = [NotNilRule(errorMessage: ErrorMessages.youMustSelectValue)]
)
or Using UIPickerView
public static func createWithPickerView(
label: String? = nil,
rules: [Rule] = [NotNilRule(errorMessage: ErrorMessages.youMustSelectValue)]
)
you can provide the values to choose, using values property
input.city.values = ["Port Said", "Damietta", "Cairo"]
and you can access the selected value like this
input.city.selectedIndex
input.city.selectedValue
Purpose: refers to input field for password
init(
shouldShowConfirmField: Bool = false,
shouldUsePasswordMeter: Bool = true,
label: String? = nil,
confirmLabel: String? = nil,
rules: [Rule]
)
Purpose: refers to input field for Agreements and consents like privacy polcicy and terms of use
init(decorator: ConsentDecorator, defaultValue: Bool = false)
you can set its delegate to control how to open links or use the default behavior
Purpose: refers to input field for Date picker
init(
defaultValue: Date? = nil,
maxDate: Date? = nil,
minDate: Date? = nil,
label: String? = nil,
rules: [Rule]
)
Purpose: refers to input field for Numbers only
init(
label: String? = nil,
rules: [Rule]
)
Purpose: refers to input field for TextViews
init(
label: String? = nil,
rules: [Rule] = [],
height: ParagraphHeight = .fixed()
)
Purpose: refers to input field for choosing multiple items files or images
init(
canPickImages: Bool = true,
canPickFiles: Bool = true,
maxNumberOfItems: Int = .max,
minNumberOfItems: Int = 1,
decorator: MultipleItemPickerDecorator = MultipleItemPickerDecorator(),
label: String? = nil
)
Purpose: refers to input field for choosing single item file or image
init(
canPickImage: Bool = true,
canPickFile: Bool = true,
isRequired: Bool = true,
decorator: SingleItemPickerDecorator = SingleItemPickerDecorator(),
label: String? = nil
)
Purpose: puts two field beside each other horizontally
init(first: FirstDescriptor, second: SecondDescriptor)
Purpose: refers to input field for adding multiple values in multiple fields
init(
minNumberOfItems: Int = 1,
maxNumberOfItems: Int = .max,
label: String? = nil,
fieldPlaceholder: String? = nil,
rules: [Rule] = []
)
Purpose: puts a custom view in the form, it requires custom handling by the developer
init(customComponent: Custom)
you can subclass CustomFormComponent and override buildComponent function like this
class CustomFieldViewComponent: CustomFormComponent {
override func buildComponent(alignments: FormAlignments) -> UIView {
let view = UIView()
view.height(100)
view.backgroundColor = .magenta
return view
}
}
You can use different options provided in FormGenerator class
var enableRealTimeValidation: Bool
var showErrorLabels: Bool
var showPlaceholders: Bool
func applyStyle(style: FormStyle)
func setAlignments(alignments: FormAlignments)
func applyDefaultSelectedValues()
you can also set the default values in the input's descriptors and make the form append thos values to the corresponding fields
Baianat, [email protected]
FormGenerator is available under the MIT license. See the LICENSE file for more info.