From 5bc0162cf216f36b765a7f2bc5227961c53c46ef Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 6 Jun 2019 21:12:36 +0200 Subject: [PATCH] + validation Signed-off-by: Johannes Theiner --- pom.xml | 6 ++++++ src/main/java/de/hsel/itech/jsf/AddressBean.java | 2 ++ src/main/java/de/hsel/itech/jsf/UserBean.java | 10 ++++++++++ src/main/webapp/register.xhtml | 3 ++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b2c9ace..7f9bb0b 100644 --- a/pom.xml +++ b/pom.xml @@ -146,6 +146,12 @@ 1.1.1 + + javax.validation + validation-api + 2.0.1.Final + + diff --git a/src/main/java/de/hsel/itech/jsf/AddressBean.java b/src/main/java/de/hsel/itech/jsf/AddressBean.java index c8e33f2..bfae7ff 100644 --- a/src/main/java/de/hsel/itech/jsf/AddressBean.java +++ b/src/main/java/de/hsel/itech/jsf/AddressBean.java @@ -9,6 +9,7 @@ import lombok.Setter; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; +import javax.validation.constraints.Size; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -41,6 +42,7 @@ public class AddressBean implements Serializable { @Getter @Setter + @Size(min = 5, max = 5) private Integer zipCode; @Getter diff --git a/src/main/java/de/hsel/itech/jsf/UserBean.java b/src/main/java/de/hsel/itech/jsf/UserBean.java index 09dac89..8390234 100644 --- a/src/main/java/de/hsel/itech/jsf/UserBean.java +++ b/src/main/java/de/hsel/itech/jsf/UserBean.java @@ -5,8 +5,12 @@ import de.hsel.itech.db.pojo.User; import lombok.Getter; import lombok.Setter; +import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; import java.io.Serializable; @ManagedBean @@ -19,10 +23,12 @@ public class UserBean implements Serializable { @Getter @Setter + @Size(min = 1) private String username; @Getter @Setter + @Pattern(regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])") private String email; @Getter @@ -65,6 +71,10 @@ public class UserBean implements Serializable { Database db = Database.getInstance(); if(username != null && email != null && password != null) { + if(db.user().get(username) != null) { + FacesContext.getCurrentInstance().addMessage("register:username", new FacesMessage("Dieser Nutzername ist bereits vergeben")); + return "register.xhtml"; + } User user = new User(email, username, password, (short) 1); db.user().insert(user); email = null; diff --git a/src/main/webapp/register.xhtml b/src/main/webapp/register.xhtml index 3dae1c5..62d75ab 100644 --- a/src/main/webapp/register.xhtml +++ b/src/main/webapp/register.xhtml @@ -11,8 +11,9 @@
- + Nutzername +
Email