Skip to content

Commit

Permalink
code review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Barreto committed Sep 26, 2016
1 parent 7aa2d18 commit 5220379
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.

Pull requests associated with this milestone can be found in this [filter](https://github.com/xmartlabs/Eureka/issues?utf8=%E2%9C%93&q=milestone%3A2.0.0%20).

We have made tons of changes in Eureka API to follow the new Swift API design guidelines.
We have made tons of changes to the Eureka API to follow the new Swift API design guidelines.
It's hard to enumerate all changes and most of them will be automatically suggested by Xcode.

We have also added to Eureka a extensible build-in validations support.
Expand Down
21 changes: 11 additions & 10 deletions Documentation/Eureka 2.0 Migration Guide.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Eureka 2.0 Migration Guide

Eureka 2.0.0 includes complete Swift 3 Compatibility and adopts the new [API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). It also includes new validations build-in feature.
Bring support to swift 3 involves some API updates to follow apple Swift API best practices, we have also changed and deprecated some API.

Most important changes will be listed below...

#### Requirements:

Eureka 2.0.0 needs Xcode 8, Swift3+ to work. Minimum supported iOS version is 8.0.

#### Changes

Eureka 2.0.0 includes complete Swift 3 Compatibility and adopts the new [API Design Guidelines](https://swift.org/documentation/api-design-guidelines/). It also includes the whole new validations build-in feature.
Bring support to swift 3 involves some API updates to follow Apple's Swift API best practices, we have also changed and deprecated some API's.

Most important changes will be listed below...


Many properties, methods, types were renamed and Xcode should suggest the fix automatically.


Expand All @@ -32,12 +35,10 @@ There are also some breaking changes related with deprecated API:

Removed APIs:

* `PostalAddressRow` and `ImageRow` was deleted.
* row has been deleted. You can find it and many other custom rows at EurekaCommunity [organization account](https://github.com/eurekaCommunity).

`highlightCell` and `unhighlightCell` callbacks were deleted, now we should use `row.isHighlighted` from cell update to check from highlighted status and make UI modification according its value.
* `PostalAddressRow` and `ImageRow` was deleted. You can find them and many other custom rows at EurekaCommunity [organization account](https://github.com/eurekaCommunity).
* `highlightCell` and `unhighlightCell` callbacks were deleted, now we should use `row.isHighlighted` from cell update to check from highlighted status and make UI modification according its value.

In case you want to do something when highligth state switches its value you can set up `onCellHighlightChanged` callback.
In case you want to do something when the row's highlighted state switches its value you can set up `onCellHighlightChanged` callback.

Custom Rows changes:

Expand Down
5 changes: 4 additions & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,10 @@ class ValidationsController: FormViewController {
<<< TextRow() {
$0.title = "Email Rule"
$0.add(rule: RuleRequired())
$0.add(rule: RuleEmail())
var ruleSet = RuleSet<String>()
ruleSet.add(rule: RuleRequired())
ruleSet.add(rule: RuleEmail())
$0.add(ruleSet: ruleSet)
$0.validationOptions = .validatesOnChangeAfterBlurred
}

Expand Down
78 changes: 77 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,83 @@ To easily get the selected row/s of a `SelectableSection` there are two methods:

### Validations

Will be documented soon...
Eureka 2.0.0 introduces the very requested build-in validations feature.

A row has a collection of `Rules` and a specific configuration that determines when validation rules should be evaluated.

There are some rules provided by default, but you can also create new ones on your own.

The provided rules are:
* RuleRequired
* RuleEmail
* RuleURL
* RuleGreaterThan, RuleGreaterOrEqualThan, RuleSmallerThan, RuleSmallerOrEqualThan
* RuleMinLength, RuleMaxLength
* RuleClosure

Let's see how to set up the validation rules.

```swift

override func viewDidLoad() {
super.viewDidLoad()
form
+++ Section(header: "Required Rule", footer: "Options: Validates on change")

<<< TextRow() {
$0.title = "Required Rule"
$0.add(rule: RuleRequired())
$0.validationOptions = .validatesOnChange
}
.cellUpdate { cell, row in
if !row.isValid {
cell.titleLabel?.textColor = .red
}
}

+++ Section(header: "Email Rule, Required Rule", footer: "Options: Validates on change after blurred")

<<< TextRow() {
$0.title = "Email Rule"
$0.add(rule: RuleRequired())
$0.add(rule: RuleEmail())
$0.validationOptions = .validatesOnChangeAfterBlurred
}
.cellUpdate { cell, row in
if !row.isValid {
cell.titleLabel?.textColor = .red
}
}

```

As you can see in the previous code snippet we can set up as many rules as we want in a row by invoking row's `add(rule:)` function.

Row also provides `func remove(ruleWithIdentifier identifier: String)` to remove a rule. In order to use it we must assign an id to the rule after creating it.

Sometimes the collection of rules we want to use on a row is the same we want to use on many other rows. In this case we can set up all validation rules using a `RuleSet` which is a collection of validation rules.

```swift
var rules = RuleSet<String>()
ruleSet.add(rule: RuleRequired())
ruleSet.add(rule: RuleEmail())

let row = TextRow() {
$0.title = "Email Rule"
$0.add(ruleSet: rules)
$0.validationOptions = .validatesOnChangeAfterBlurred
}
```

Eureka allows us to specify when validation rules should be evaluated. We can do it by setting up `validationOptions` row's property, which can have the following values:


* `.validatesOnChange` - Validates whenever a row value changes.
* `.validatesOnBlur` - (Default value) validates right after the cell resigns first responder. Not applicable for all rows.
* `.validatesOnChangeAfterBlurred` - Validates whenever the row value changes after it resigns first responder for the first time.
* `.validatesOnDemand` - We should manually validate the row or form by invoking `validate()` method.

If you want to validate the entire form (all the rows) you can manually invoke Form `validate()` method.

## Custom rows

Expand Down
6 changes: 6 additions & 0 deletions Source/Core/Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import Foundation

public struct ValidationError: Equatable {
public let msg: String?

public init(msg: String? = nil) {
self.msg = msg
}
}

public func ==(lhs: ValidationError, rhs: ValidationError) -> Bool{
Expand Down Expand Up @@ -71,6 +75,8 @@ public struct RuleSet<T: Equatable> {

internal var rules: [ValidationRuleHelper<T>] = []

public init(){}

public mutating func add<Rule: RuleType>(rule: Rule) where T == Rule.RowValueType{
let validFn: ((T?) -> ValidationError?) = { (val: T?) in
return rule.isValid(value: val)
Expand Down

0 comments on commit 5220379

Please sign in to comment.