effad / ValidatorFX

A form validation library for JavaFX
BSD 3-Clause "New" or "Revised" License
221 stars 19 forks source link

Add Color Changeable Graphic To Denote a required field #35

Open colindj1120 opened 1 year ago

colindj1120 commented 1 year ago

Allow someone to set a field as required which shows some icon graphic that they can change the color of.

effad commented 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).

colindj1120 commented 1 year ago

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.

reardenSteel1964 commented 11 months ago

A required field indicator like this would be nice addition. +1.

effad commented 6 months ago

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).

effad commented 6 months ago

TODO: I should write about the DefaultDecoration in the README.md ...