Provide default checks
Some checks (field required, max input length, etc.) are so common we should provide convenience ways of adding them
yes that might be extremely helpful for not duplicate code all over
Since warning/error-messages must be translatable (i18n), these messages must always be provided by the client of ValidatorFX.
Here are things that I would consider common checks:
- Field is required (i.e. it must not be empty)
- Field must have a minimum length
- Field must not exceed a maximum length
- Field must be a number (separate checks for int/long/double, etc.)
- With min/max value range
- Field must match a regular expression
Most of these checks may only be applied to TextInputControls (i.e. TextField/TextArea), since its textProperty() will be checked.
The required-check should be applicable to all kinds of Controls but will require separate convenience-methods for different kinds of controls (TextInputControls must be checked against textProperty().isBlank(); ComboBox must be checked against valueProperty() != null, etc.)
@georgemoralis If you have more suggestions as to what you would expect as convenience methods, please tell me.
i have done some validators using controlsfx validator you can check here
https://github.com/codebbgr/protoERP/blob/main/src/main/java/gr/codebb/protoerp/util/validation/Validators.java
probably something similar can be done here :D
Considering the following piece of code:
validator.createCheck()
.dependsOn("usePatientId", usePatientId.selectedProperty())
.dependsOn("useName", useName.selectedProperty())
.dependsOn("exactBirthDate", exactBirthDate.selectedProperty())
.dependsOn("theFirst", theFirst.selectedProperty())
.withMethod(c -> {
if (!(boolean) c.get("usePatientId") && !(boolean) c.get("useName") && !(boolean) c.get("exactBirthDate") && !(boolean) c.get("theFirst")) {
c.error(LC.MustSelectAtLeastOneCriterion());
}
})
.immediate()
;
it would be nice to:
- be able to iterate over all the values of a Check.Context (not only .get them).
- have a default-function to check that one of the boolean properties we depend on must be true.
This would allow :
validator.createCheck()
.dependsOn("usePatientId", usePatientId.selectedProperty())
.dependsOn("useName", useName.selectedProperty())
.dependsOn("exactBirthDate", exactBirthDate.selectedProperty())
.dependsOn("theFirst", theFirst.selectedProperty())
.withMethod(c -> atLeastOneMustBeTrue(c))
.immediate()
;