-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from sialcasa/194_validation
194 validation
- Loading branch information
Showing
7 changed files
with
186 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...c/test/java/de/saxsys/mvvmfx/utils/validation/crossfieldexample/CrossFieldExampleApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.saxsys.mvvmfx.utils.validation.crossfieldexample; | ||
|
||
import de.saxsys.mvvmfx.FluentViewLoader; | ||
import javafx.application.Application; | ||
import javafx.scene.Parent; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.PasswordField; | ||
import javafx.scene.layout.GridPane; | ||
import javafx.stage.Stage; | ||
|
||
/** | ||
* @author manuel.mauky | ||
*/ | ||
public class CrossFieldExampleApp extends Application { | ||
|
||
public static void main(String... args) { | ||
launch(args); | ||
} | ||
|
||
|
||
@Override | ||
public void start(Stage primaryStage) throws Exception { | ||
final Parent root = FluentViewLoader.fxmlView(RegisterFormView.class).load().getView(); | ||
primaryStage.setScene(new Scene(root)); | ||
primaryStage.show(); | ||
|
||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...x/src/test/java/de/saxsys/mvvmfx/utils/validation/crossfieldexample/RegisterFormView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.saxsys.mvvmfx.utils.validation.crossfieldexample; | ||
|
||
import de.saxsys.mvvmfx.FxmlView; | ||
import de.saxsys.mvvmfx.InjectViewModel; | ||
import de.saxsys.mvvmfx.utils.validation.ValidationMessage; | ||
import javafx.application.Platform; | ||
import javafx.collections.ListChangeListener; | ||
import javafx.event.ActionEvent; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.PasswordField; | ||
|
||
/** | ||
* @author manuel.mauky | ||
*/ | ||
public class RegisterFormView implements FxmlView<RegisterFormViewModel> { | ||
|
||
@FXML | ||
public PasswordField pwInput; | ||
@FXML | ||
public PasswordField pwRepeatInput; | ||
@FXML | ||
public Label message; | ||
@FXML | ||
public Button okButton; | ||
|
||
@InjectViewModel | ||
private RegisterFormViewModel viewModel; | ||
|
||
|
||
public void initialize() { | ||
|
||
viewModel.passwordProperty().bindBidirectional(pwInput.textProperty()); | ||
viewModel.passwordRepeatProperty().bindBidirectional(pwRepeatInput.textProperty()); | ||
|
||
updateMessage(); | ||
|
||
viewModel.getValidation().getMessages().addListener((ListChangeListener<ValidationMessage>) c -> { | ||
updateMessage(); | ||
}); | ||
okButton.disableProperty().bind(viewModel.getValidation().validProperty().not()); | ||
|
||
} | ||
|
||
private void updateMessage() { | ||
Platform.runLater(() -> message.setText( | ||
viewModel.getValidation().getHighestMessage().map(ValidationMessage::getMessage) | ||
.orElse("everythink ok"))); | ||
} | ||
|
||
public void ok() { | ||
System.out.println("Register!"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
.../test/java/de/saxsys/mvvmfx/utils/validation/crossfieldexample/RegisterFormViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.saxsys.mvvmfx.utils.validation.crossfieldexample; | ||
|
||
import de.saxsys.mvvmfx.ViewModel; | ||
import de.saxsys.mvvmfx.utils.validation.ObservableRuleBasedValidator; | ||
import de.saxsys.mvvmfx.utils.validation.ValidationMessage; | ||
import de.saxsys.mvvmfx.utils.validation.ValidationStatus; | ||
import javafx.beans.binding.Bindings; | ||
import javafx.beans.binding.BooleanBinding; | ||
import javafx.beans.property.ReadOnlyStringWrapper; | ||
import javafx.beans.property.SimpleStringProperty; | ||
import javafx.beans.property.StringProperty; | ||
|
||
/** | ||
* @author manuel.mauky | ||
*/ | ||
public class RegisterFormViewModel implements ViewModel { | ||
|
||
private StringProperty password = new SimpleStringProperty(); | ||
private StringProperty passwordRepeat = new SimpleStringProperty(); | ||
|
||
|
||
private ObservableRuleBasedValidator passwordValidator = new ObservableRuleBasedValidator(); | ||
|
||
public RegisterFormViewModel() { | ||
final BooleanBinding rule1 = password.isNotEmpty(); | ||
final BooleanBinding rule2 = passwordRepeat.isNotEmpty(); | ||
final BooleanBinding rule3 = password.isEqualTo(passwordRepeat); | ||
|
||
passwordValidator.addRule(rule1, ValidationMessage.error("Please enter a password")); | ||
passwordValidator.addRule(rule2, ValidationMessage.error("Please enter the password a second time")); | ||
passwordValidator.addRule(rule3, ValidationMessage.error("Both passwords need to be the same")); | ||
} | ||
|
||
public ValidationStatus getValidation() { | ||
return passwordValidator.getValidationStatus(); | ||
} | ||
|
||
public String getPassword() { | ||
return password.get(); | ||
} | ||
|
||
public StringProperty passwordProperty() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password.set(password); | ||
} | ||
|
||
public String getPasswordRepeat() { | ||
return passwordRepeat.get(); | ||
} | ||
|
||
public StringProperty passwordRepeatProperty() { | ||
return passwordRepeat; | ||
} | ||
|
||
public void setPasswordRepeat(String passwordRepeat) { | ||
this.passwordRepeat.set(passwordRepeat); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
.../test/resources/de/saxsys/mvvmfx/utils/validation/crossfieldexample/RegisterFormView.fxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?import javafx.scene.control.*?> | ||
<?import java.lang.*?> | ||
<?import javafx.scene.layout.*?> | ||
|
||
|
||
<AnchorPane fx:controller="de.saxsys.mvvmfx.utils.validation.crossfieldexample.RegisterFormView" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"> | ||
<children> | ||
<GridPane layoutX="53.0" layoutY="116.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> | ||
<columnConstraints> | ||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="294.0" minWidth="10.0" prefWidth="149.0" /> | ||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="451.0" minWidth="10.0" prefWidth="451.0" /> | ||
</columnConstraints> | ||
<rowConstraints> | ||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | ||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | ||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | ||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | ||
</rowConstraints> | ||
<children> | ||
<Button fx:id="okButton" mnemonicParsing="false" onAction="#ok" text="Button" GridPane.columnIndex="1" GridPane.rowIndex="3" /> | ||
<Label text="Password:" /> | ||
<Label text="Repeat Password:" GridPane.rowIndex="1" /> | ||
<PasswordField fx:id="pwInput" GridPane.columnIndex="1" /> | ||
<PasswordField fx:id="pwRepeatInput" GridPane.columnIndex="1" GridPane.rowIndex="1" /> | ||
<Label fx:id="message" GridPane.columnSpan="2" GridPane.rowIndex="2" /> | ||
</children> | ||
</GridPane> | ||
</children> | ||
</AnchorPane> |