-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Comments
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. |
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 |
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 |
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.) |
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. |
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 |
Closed by #48 |
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.
The text was updated successfully, but these errors were encountered: