Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GameplayJDK committed Jul 10, 2017
0 parents commit 89ec42d
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/out
*.iml
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<AnchorPane> implements Requirement<StringProperty> {

@FXML
private TextField textField;

private Validator<StringProperty> stringValidator;

public MyController() {
super();
}

@FXML
@Override
protected void initialize() {
this.stringValidator = new Validator<StringProperty>(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.
11 changes: 11 additions & 0 deletions src/de/gameplayjdk/validatorfx/Requirement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.gameplayjdk.validatorfx;

import javafx.beans.property.Property;

/**
* Created by GameplayJDK on 07.06.2017.
*/
public interface Requirement<P extends Property> {

boolean check(P property);
}
61 changes: 61 additions & 0 deletions src/de/gameplayjdk/validatorfx/Validator.java
Original file line number Diff line number Diff line change
@@ -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<P extends Property> {

private ObjectProperty<P> property;

private ObjectProperty<Requirement<P>> requirement;

public Validator() {
this(null, null);
}

public Validator(P property) {
this(property, null);
}

public Validator(P property, Requirement<P> requirement) {
this.property = new SimpleObjectProperty<P>(property);

this.requirement = new SimpleObjectProperty<Requirement<P>>(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<P> propertyProperty() {
return property;
}

public void setProperty(P property) {
this.property.set(property);
}

public Requirement<P> getRequirement() {
return this.requirement.get();
}

public ObjectProperty<Requirement<P>> requirementProperty() {
return requirement;
}

public void setRequirement(Requirement<P> requirement) {
this.requirement.set(requirement);
}
}
47 changes: 47 additions & 0 deletions src/de/gameplayjdk/validatorfx/ValidatorGroup.java
Original file line number Diff line number Diff line change
@@ -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<P extends Property> extends Validator<MapProperty<String, Validator<P>>> implements Requirement<MapProperty<String, Validator<P>>> {

private MapProperty<String, Validator<P>> mapProperty;

public ValidatorGroup() {
super();

this.mapProperty = new SimpleMapProperty<String, Validator<P>>();
this.mapProperty.setValue(FXCollections.observableHashMap());

super.setProperty(this.mapProperty);
super.setRequirement(this);
}

@Override
public boolean check(MapProperty<String, Validator<P>> property) {
boolean valid = true;

for (Validator<P> validator : this.mapProperty.values()) {
if (validator == this) {
continue;
}

valid = valid && validator.validate();
}

return valid;
}

public void addValidator(String id, Validator<P> validator) {
this.mapProperty.put(id, validator);
}

public void removeValidator(String id) {
this.mapProperty.remove(id);
}
}

0 comments on commit 89ec42d

Please sign in to comment.