Skip to content

Instantly share code, notes, and snippets.

@hendrikebbers
Created November 7, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrikebbers/3a4fb58c5384638b6b34affe3b6b0228 to your computer and use it in GitHub Desktop.
Save hendrikebbers/3a4fb58c5384638b6b34affe3b6b0228 to your computer and use it in GitHub Desktop.
public class FormController extends AbstractFormController {
private final Validator validator;
private ValidatedValues<FormModel> v;
public FormController(Validator validator) {
this.validator = validator;
v = ValidatedValues.build(FormModel.class).
withProperty("firstName", () -> getFirstNameTextField().getText(), v -> updateForValidation(getFirstNameTextField(), v)).
withProperty("lastName", () -> getLastNameTextField().getText(), v -> updateForValidation(getLastNameTextField(), v)).
withProperty("active", () -> getActiveCheckBox().isSelected(), v -> updateForValidation(getActiveCheckBox(), v)).
withProperty("gender", () -> getGenderChoiceBox().getValue(), v -> updateForValidation(getGenderChoiceBox(), v));
}
@Override
protected void validate() {
Set<ConstraintViolation<FormModel>> violations = validator.validate(v, UIGroup.class);
if(violations == null || violations.isEmpty()) {
System.out.println("We have no violations!");
FormModel model = updateModelByUI();
// Send model to server...
} else {
System.out.println("We have violations!");
violations.forEach(v -> System.out.println(v.getMessage()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment