Open colindj1120 opened 1 year ago
I am not sure I understand your requirement correctly. This can easily be done, like this:
...
TextField userTextField = new TextField();
validator.createCheck()
.withMethod(this::required)
.dependsOn("text", userTextField.textProperty())
.decorates(userTextField)
.immediate()
...
private void required(Context context) {
String text = context.get("text");
if (text == null || text.isEmpty()) {
context.error("This field is required.");
}
}
Note that required may use text.isBlank() instead of isEmpty if you want to disallow all-whitespace input. And depending on your type of Control you may need to implement things differently (e.g. a ComboBox has a valueProperty, not a textProperty).
I was thinking in the terms of Controls FX where if you set the required flag to true there is a little red caret in the top right corner of the text box. I hadn't found a way to change the caret color or caret icon type. This caret stays there indefinitely and then the little circle red x icon is displayed in the bottom right corner when the validation fails. I was wondering if ValidatorFX allowed the same and also allowed for changing of the icon/color.
A required field indicator like this would be nice addition. +1.
This can already be done by providing your own default decoration factory: Call (net.synedra.validatorfx.DefaultDecoration.setFactory(Function<ValidationMessage, Decoration>) and pass it a factory that will create its own GraphicDecoration (using whatever icon you have / want).
TODO: I should write about the DefaultDecoration in the README.md ...
Allow someone to set a field as required which shows some icon graphic that they can change the color of.