Skip to content

Create Binding+Validation.swift - #1

Merged
mkj-is merged 2 commits into
mkj-is:mainfrom
malhal:main
Aug 17, 2026
Merged

Create Binding+Validation.swift#1
mkj-is merged 2 commits into
mkj-is:mainfrom
malhal:main

Conversation

@malhal

@malhal malhal commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@mkj-is mkj-is left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, I added one comment regarding the return type. What do you think?

Comment thread Sources/BindingKit/Binding+Validation.swift Outdated
@malhal

malhal commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Good point, we probably only need the bool version and I will return false when I call a throwing func that fails.

@malhal

malhal commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

E.g. so now I can do:

.validate { title in
    (try? book.validateValue(title, for: \.title)) != nil
}

@malhal

malhal commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Maybe now it should be renamed filter?

@mkj-is

mkj-is commented Aug 17, 2026

Copy link
Copy Markdown
Owner

Maybe now it should be renamed filter?

I am fine with validate. Filter might be confusing as it filters values only one way (set not get).

@mkj-is
mkj-is merged commit 13a5a61 into mkj-is:main Aug 17, 2026
1 check failed
@malhal

malhal commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

I thought I'd share something interesting I noticed about TextField's value binding when using a computed Binding. Say you have "1" in the TextField and you select all and paste "2A" the binding setter is called with 2. If you had however pasted "A2" then the binding is set with 1, i.e. the old value.

This makes me wonder if we should be setting the wrapped value to the original value when validation fails?

Edit: seems to me TextField's value behaviour has not been implemented correctly. If the parsing fails it should set nil on an optional but it actually resets the last valid value. This means the textfield goes out of sync with the model, i.e. a save button might be enabled because the model's values are valid yet the textfield contains junk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants