Feature request: disable OK button in dialog if textToValue returns null in TextFieldPreference
Hello. Thanks for the great library!
Currently, if the textToValue lambda retuns null pressing the OK button has no effect. This is a kinda roundabout way of validating the input, but it yields a poor UX.
Would you consider disabling the OK button in this case?
This would require calculating textToValue at each value update, so maybe a different isValid lambda could be provided for this use-case.
Thanks!
The current code actually isn't meant to provide input validation out of the box - generally input validation should be provided as a text field error message. So I believe you can provide your own textField and provide error message there instead.
Hey, thanks for the quick answer!
Yeah that's true, however I was aiming at having something less intrusive, as in my specific case the only validation I'm making is that the string is not blank. I think simply disabling the ok button would suffice.
Maybe the input validation angle is incorrect, but there is no way currently to tell the dialog created by this preference to disable the ok button. Maybe there should be a parameter for this?
For now I've ended up using a simple Preference and recreating the whole alert dialog. I'm not using any data store for this so it's not a big deal.
Edit: or maybe a similar way to override the dialog buttons, like we do for the text field?
I think you'll need to create a new local state for whether okay button is enabled, and update that upon text change. I think this will be getting more complicated than a simple API and different library users will always have their own use case that needs something different. So I feel if one needs validation, they should just create their own preference instead.
I'm already using the TextFieldPreference that accepts a value, so I'm already keeping track of it.
Wouldn't simply having a okButton: @Composable (value: TextFieldValue, onOk: () -> Unit) paramter suffice?
This could also serve as a way to customize the appearence of the OK button (e.g. make it red for dangerous actions), etc.