+ validation

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-06-06 21:12:36 +02:00
parent 41d62898c1
commit 5bc0162cf2
4 changed files with 20 additions and 1 deletions

View File

@ -146,6 +146,12 @@
<version>1.1.1</version> <version>1.1.1</version>
</dependency> </dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!--Database--> <!--Database-->
<dependency> <dependency>

View File

@ -9,6 +9,7 @@ import lombok.Setter;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.validation.constraints.Size;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -41,6 +42,7 @@ public class AddressBean implements Serializable {
@Getter @Getter
@Setter @Setter
@Size(min = 5, max = 5)
private Integer zipCode; private Integer zipCode;
@Getter @Getter

View File

@ -5,8 +5,12 @@ import de.hsel.itech.db.pojo.User;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable; import java.io.Serializable;
@ManagedBean @ManagedBean
@ -19,10 +23,12 @@ public class UserBean implements Serializable {
@Getter @Getter
@Setter @Setter
@Size(min = 1)
private String username; private String username;
@Getter @Getter
@Setter @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; private String email;
@Getter @Getter
@ -65,6 +71,10 @@ public class UserBean implements Serializable {
Database db = Database.getInstance(); Database db = Database.getInstance();
if(username != null && email != null && password != null) { 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); User user = new User(email, username, password, (short) 1);
db.user().insert(user); db.user().insert(user);
email = null; email = null;

View File

@ -11,8 +11,9 @@
<div class="m-row"> <div class="m-row">
<div class="m-col-l-6 m-push-l-4"> <div class="m-col-l-6 m-push-l-4">
<h:form> <h:form id="register">
<h:inputText id="username" value="#{userBean.username}">Nutzername</h:inputText> <h:inputText id="username" value="#{userBean.username}">Nutzername</h:inputText>
<h:message for="username"/>
<br/> <br/>
<h:inputText id="email" value="#{userBean.email}">Email</h:inputText> <h:inputText id="email" value="#{userBean.email}">Email</h:inputText>
<br/> <br/>