diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..810fe03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +/out +*.iml diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..3708e94 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 GameplayJDK + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9d2300 --- /dev/null +++ b/README.md @@ -0,0 +1,107 @@ +# validator.fx + +Simple JavaFx library to handle field validation (or validation of properties in general). + +The idea behind this library is to allow validation of all kinds of properties and structure said validation as object tree using only one `validate()` method to check all. + +## What's in it? + +The library only contains three classes which makes its size only about `4kb`. + + - The `Validator` is simply a base class that wraps an `ObjectProperty` and allows a check against a `Requirement`. + - A `Requirement` allows definition of a method for checking any type of property and return the fulfillment as boolean. + - With `ValidatorGroup` is itself an extended implementation of a `Validator` that allows to use multiple `Validators` at once. + +## How to install it? + +For that just download the latest release and add the jar to your project as a library. + +Of course you are free to clone the repo and build it yourself. + +## How to use it? + +**Please also read the section with other important information below!** + +(The following example use code from [simple.fx](https://github.com/GameplayJDK/simple.fx)). + +To use the library simply instantiate a `Validator` and define a requirement method for it: + +```java +// ... + +import javafx.fxml.FXML; +import javafx.scene.layout.AnchorPane; + +import de.gameplayjdk.simplefx.Controller; + +public class MyController extends Controller implements Requirement { + + @FXML + private TextField textField; + + private Validator stringValidator; + + public MyController() { + super(); + } + + @FXML + @Override + protected void initialize() { + this.stringValidator = new Validator(this.textField.textProperty(), this); + + // ... + } + + // The validation logic + @Override + public boolean check(StringProperty property) { + return !property.isEmpty(); + } + + // The validation flow (call this before submitting, etc) + public void validateTextField() { + if (!this.stringValidator.validate()) { + // Field is empty + + return; + } + + // ... + } + + // ... +} + +// ... +``` + +You can avoid having too many validators in your controller by using the `validate(P property)` method, which accepts a property to check as argument. + +The possibility to group validators is also an alternative: + +```java +// ... + + public void initializeValidator() { + ValidatorGroup group = new ValidatorGroup(); + + Validator nameValidator = new Validator(this.textField.textProperty(), ...); + Validator tocValidator = new Validator(this.checkBox.selectedProperty(), ...); + + group.addValidator("Name", nameValidator); + group.addValidator("AcceptTOC", tocValidator); + // Use `group.removeValidator(...)` to remove a validator from the group + + // ... + + // Returns the combined reslt of all containing validators + group.validate(); + } + +// ... +``` + +## And the license? + +It's MIT. diff --git a/src/de/gameplayjdk/validatorfx/Requirement.java b/src/de/gameplayjdk/validatorfx/Requirement.java new file mode 100644 index 0000000..448ea0e --- /dev/null +++ b/src/de/gameplayjdk/validatorfx/Requirement.java @@ -0,0 +1,11 @@ +package de.gameplayjdk.validatorfx; + +import javafx.beans.property.Property; + +/** + * Created by GameplayJDK on 07.06.2017. + */ +public interface Requirement

{ + + boolean check(P property); +} diff --git a/src/de/gameplayjdk/validatorfx/Validator.java b/src/de/gameplayjdk/validatorfx/Validator.java new file mode 100644 index 0000000..d32db6a --- /dev/null +++ b/src/de/gameplayjdk/validatorfx/Validator.java @@ -0,0 +1,61 @@ +package de.gameplayjdk.validatorfx; + +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.Property; +import javafx.beans.property.SimpleObjectProperty; + +/** + * Created by GameplayJDK on 07.06.2017. + */ +public class Validator

{ + + private ObjectProperty

property; + + private ObjectProperty> requirement; + + public Validator() { + this(null, null); + } + + public Validator(P property) { + this(property, null); + } + + public Validator(P property, Requirement

requirement) { + this.property = new SimpleObjectProperty

(property); + + this.requirement = new SimpleObjectProperty>(requirement); + } + + public boolean validate(P property) { + return this.getRequirement().check(property); + } + + public boolean validate() { + return this.validate(this.getProperty()); + } + + public P getProperty() { + return this.property.get(); + } + + public ObjectProperty

propertyProperty() { + return property; + } + + public void setProperty(P property) { + this.property.set(property); + } + + public Requirement

getRequirement() { + return this.requirement.get(); + } + + public ObjectProperty> requirementProperty() { + return requirement; + } + + public void setRequirement(Requirement

requirement) { + this.requirement.set(requirement); + } +} diff --git a/src/de/gameplayjdk/validatorfx/ValidatorGroup.java b/src/de/gameplayjdk/validatorfx/ValidatorGroup.java new file mode 100644 index 0000000..3b0fbc3 --- /dev/null +++ b/src/de/gameplayjdk/validatorfx/ValidatorGroup.java @@ -0,0 +1,47 @@ +package de.gameplayjdk.validatorfx; + +import javafx.beans.property.MapProperty; +import javafx.beans.property.Property; +import javafx.beans.property.SimpleMapProperty; +import javafx.collections.FXCollections; + +/** + * Created by GameplayJDK on 07.06.2017. + */ +public class ValidatorGroup

extends Validator>> implements Requirement>> { + + private MapProperty> mapProperty; + + public ValidatorGroup() { + super(); + + this.mapProperty = new SimpleMapProperty>(); + this.mapProperty.setValue(FXCollections.observableHashMap()); + + super.setProperty(this.mapProperty); + super.setRequirement(this); + } + + @Override + public boolean check(MapProperty> property) { + boolean valid = true; + + for (Validator

validator : this.mapProperty.values()) { + if (validator == this) { + continue; + } + + valid = valid && validator.validate(); + } + + return valid; + } + + public void addValidator(String id, Validator

validator) { + this.mapProperty.put(id, validator); + } + + public void removeValidator(String id) { + this.mapProperty.remove(id); + } +}