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

Validation #194

Closed
manuel-mauky opened this issue Apr 1, 2015 · 4 comments
Closed

Validation #194

manuel-mauky opened this issue Apr 1, 2015 · 4 comments
Assignees
Labels
Milestone

Comments

@manuel-mauky
Copy link
Collaborator

No description provided.

@sialcasa sialcasa added this to the 1.1.0 milestone Apr 2, 2015
manuel-mauky added a commit that referenced this issue Apr 7, 2015
@sialcasa
Copy link
Owner

sialcasa commented Apr 8, 2015

Check this too: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html

Maybe this is an option for the internal implementation of a validator

Example for a 5-Char Textfield:

TextFormatter<Object> textFormatter = new TextFormatter<>(t -> {
            if (t.getControlNewText().length() > 5) {
                t.setText(""); // negate the change
            }
            return t;
        });

firstnameInput.setTextFormatter(textFormatter);

Example for a Validator implementation with textformatter:

import java.util.function.Predicate;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextInputControl;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ValidationApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField textField = new TextField();
        Button button = new Button("Ok");
        VBox vbox = new VBox(textField, button);

        Validator validator = new Validator(t -> t.length() < 5);
        validator.bindValidation(textField);
        validator.validProperty().addListener((ChangeListener<Boolean>) (observable, oldValue, newValue) -> {
            if (newValue) {
                textField.setStyle("-fx-background-color:-fx-base;");
            } else {
                textField.setStyle("-fx-background-color:red;");
            }
        });

        button.disableProperty().bind(validator.validProperty().not());

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    class Validator {

        ReadOnlyBooleanWrapper validProperty = new ReadOnlyBooleanWrapper();

        private final Predicate<String> validator;

        public Validator(Predicate<String> validator) {
            this.validator = validator;
        }


        public ReadOnlyBooleanProperty validProperty() {
            return validProperty.getReadOnlyProperty();
        }


        public final void bindValidation(TextInputControl textInput) {
            TextFormatter<String> textFormatter = new TextFormatter<>(t -> {
                String newText = t.getControlNewText();
                validProperty.set(this.validator.test(newText));
                return t;
            });

            textInput.setTextFormatter(textFormatter);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

}

@manuel-mauky manuel-mauky modified the milestones: 1.2.0, 1.1.0 Apr 9, 2015
@sialcasa
Copy link
Owner

Bean Validation Support

manuel-mauky added a commit that referenced this issue May 28, 2015
manuel-mauky added a commit that referenced this issue Jun 2, 2015
manuel-mauky added a commit that referenced this issue Jun 9, 2015
@manuel-mauky
Copy link
Collaborator Author

The implementation of the first version is done. But before we can close this issue we need to add documentation to the wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants