ValidatorFX icon indicating copy to clipboard operation
ValidatorFX copied to clipboard

Provide default checks

Open effad opened this issue 6 years ago • 4 comments

Some checks (field required, max input length, etc.) are so common we should provide convenience ways of adding them

effad avatar Dec 12 '19 13:12 effad

yes that might be extremely helpful for not duplicate code all over

georgemoralis avatar Mar 16 '21 14:03 georgemoralis

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.

effad avatar Mar 17 '21 07:03 effad

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

georgemoralis avatar Mar 20 '21 12:03 georgemoralis

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

effad avatar Jul 12 '22 12:07 effad