Skip to content

Commit

Permalink
Merge pull request #256 from sialcasa/194_validation
Browse files Browse the repository at this point in the history
194 validation
  • Loading branch information
manuel-mauky committed Jun 10, 2015
2 parents 40803e2 + ef379e9 commit 05a3d14
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ContactFormViewModel() {
});


formValidator.registerValidator(
formValidator.addValidators(
firstnameValidator,
lastnameValidator,
emailValidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public CompositeValidator() {
}

public CompositeValidator(Validator... validators) {
registerValidator(validators);
addValidators(validators);
}


public void registerValidator(Validator... validators) {
public void addValidators(Validator... validators) {
result.addResults(Stream.of(validators)
.map(Validator::getValidationStatus)
.collect(Collectors.toList()));
}

public void unregisterValidator(Validator... validators) {
public void removeValidators(Validator... validators) {
result.removeResults(Stream.of(validators)
.map(Validator::getValidationStatus)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setup() {

@Test
public void testValidation() {
compositeValidator.registerValidator(validator1, validator2);
compositeValidator.addValidators(validator1, validator2);

valid1.set(true);
valid2.set(true);
Expand Down Expand Up @@ -79,13 +79,13 @@ public void testLazyRegistration() {

assertThat(status.isValid()).isTrue(); // no validator is registered at the moment

compositeValidator.registerValidator(validator2);
compositeValidator.addValidators(validator2);
assertThat(status.isValid()).isTrue(); // validator2 is valid

compositeValidator.registerValidator(validator1);
compositeValidator.addValidators(validator1);
assertThat(status.isValid()).isFalse();

compositeValidator.unregisterValidator(validator1);
compositeValidator.removeValidators(validator1);
assertThat(status.isValid()).isTrue();
}

Expand Down
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();

}
}
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!");
}
}
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);
}

}
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>

0 comments on commit 05a3d14

Please sign in to comment.