Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use invalid input in update function? #45

Closed
cmeeren opened this issue Aug 7, 2018 · 7 comments
Closed

How to use invalid input in update function? #45

cmeeren opened this issue Aug 7, 2018 · 7 comments

Comments

@cmeeren
Copy link
Member

cmeeren commented Aug 7, 2018

Consider a form with a single input field. It must be non-empty to be valid. If it's valid, the Submit button is enabled; if it's invalid, the button is disabled.

This simple (and I'd guess common) use case seems impossible in Elmish.WPF.

The reason: AFAIK, in Elmish.WPF, Binding.twoWayValidation is the only way to trigger validation messages. twoWayValidation only sends a message when the input is valid. This means that changes to an input that moves the input from valid to invalid are not sent, and thus validation state (or invalid input) cannot be a part of the model.

Unless I'm mistaken, this means that it's impossible to e.g. disable a button whenever the input is invalid, because if the user moves from a valid state (text in the input field) to an invalid state (empty input field), no message is sent and the state keeps the (now outdated) previous valid value, happily ignorant that the input field is now in fact invalid.

In short, there needs to be another way to trigger validation in Elmish.WPF that still allows messages (which can then contain the raw, invalid input) to be sent to Elmish even for invalid input.

@et1975
Copy link
Member

et1975 commented Aug 7, 2018

I must be missing something, because one can validate on every update, the invalid value then becomes part of the state along with the validation results, which you'd bind to some other element.

@cmeeren
Copy link
Member Author

cmeeren commented Aug 7, 2018

I can keep the raw (potentially invalid) state in the model as I can any other plain old value, but in that case I must use Binding.twoWay instead of Binding.twoWayValidation (because the latter won't send invalid values back into the model), and I don't know of any other (Elmish.WPF-friendly) ways to trigger validation error messages for input controls. Do you have any? (That preferably don't require a boatload of boilerplate XAML?)

@et1975
Copy link
Member

et1975 commented Aug 7, 2018

I see, you want WPF-specific validation to kick in. I personally haven't touched WPF in a while, maybe @2sComplement can comment when he's back from vacation. But...

Here's my (somewhat dated) take on the validation: https://thoughtspam.wordpress.com/2013/09/02/starting-an-open-source-rewrite-of-a-validation-microframework/

The gist of it is, describing your validation rules in terms of your domain and independently from presentation technology has many advantages and that's the suggested "elmish" way. We haven't implemented anything specific to support this, but others have, here's an example: https://github.com/zaaack/fable-validation

@cmeeren
Copy link
Member Author

cmeeren commented Aug 7, 2018

Yes you're right, I want to trigger WPF validation (because with MaterialDesignInXamlToolkit, which I'm using, it looks great and sticks to Material Design - also it's much less boilerplate than adding my own controls for validation messages for each input).

I may have missed something about your blog post, because I didn't see the relevance in this context. (Besides, I already have domain functions that I compose to validate/transform each input, each producing Result<_,string> with a relevant error message. AFAIK you don't get much more specific validation context than input-specific validation functions and error messages based on transforming raw user input to your domain types. It's completely independent from presentation technology and only assumes that there exists some text field for the user to type into.)

@et1975
Copy link
Member

et1975 commented Aug 7, 2018

If you already have validation results based on the model, what's stopping you from storing that in the state and binding them to the error controls? I'm assuming some glue might be required between the two, but it sounds like you're in good position to figure it out.

@cmeeren
Copy link
Member Author

cmeeren commented Aug 8, 2018

My lack of experience with WPF might be the culprit here, but AFAIK there is no way to directly bind validation status / error messages of a control to a VM property; in order to trigger WPF validation, I have to implement IDataErrorInfo or INotifyDataErrorInfo in the view model (which in this case is controlled by Elmish.WPF, so it's impossible for users to do anything about), or else use validation rules (which has severe limitations in MVU because it does not make the validation a part of the model/update). My searches have not led me to other insights. If this is wrong, I'll happily take a hint as to how I can trigger WPF validation outside of these three. :)

@cmeeren
Copy link
Member Author

cmeeren commented Sep 1, 2018

Closed by #48

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

No branches or pull requests

2 participants