I'm following an example given by my teacher but when I implement it @NotNull doesn't work along with @Pattern for first and last name.
For @NotNull I want to display a message next to the input box with an error. For some reason as the code is here it doesn't display an error.
For @Pattern I am trying to check if the first or last name has a number in it and if it does return an error. I have tried looking at this pattern regexp constructs but can't seem to get it to work properly.
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb"
xmlns:h="http://ift.tt/HjFrZb"
xmlns:f="http://ift.tt/HcrI1S">
<h:head>
<title>Sign Up</title>
</h:head>
<h:body>
<h:form id="componentForm">
<h:panelGrid columns="3">
Last name:
<h:inputText id="lastName" value="#{register.lastName}"/>
<h:messages for="lastName" errorStyle="color: red" infoStyle="color: green"/>
First name:
<h:inputText id="firstName" value="#{register.firstName}"/>
<h:messages for="firstName" errorStyle="color: red" infoStyle="color: green"/>
Password:
<h:inputSecret id="password" value="#{register.password}"/>
<h:messages for="password" errorStyle="color: red" infoStyle="color: green"/>
Email:
<h:inputText id="email" value="#{register.email}"/>
<h:messages for="email" errorStyle="color: red" infoStyle="color: green"/>
Phone number:
<h:inputText id="phoneNumber" value="#{register.phoneNumber}"/>
<h:messages for="phoneNumber" errorStyle="color: red" infoStyle="color: green"/>
Gender:
<h:selectOneRadio id="gender" value="#{register.gender}">
<f:selectItems value="#{register.genderItems}"/>
</h:selectOneRadio>
<h:messages for="gender" errorStyle="color: red" infoStyle="color: green"/>
Languages:
<h:selectManyCheckbox id="languages" value="#{register.languages}">
<f:selectItems value="#{register.languageItems}"/>
</h:selectManyCheckbox>
<h:messages for="languages" errorStyle="color: red" infoStyle="color: green"/>
Hometown:
<h:selectOneMenu id="hometown" value="#{register.hometown}">
<f:selectItems value="#{register.hometownItems}"/>
</h:selectOneMenu>
<h:messages for="hometown" errorStyle="color: red" infoStyle="color: green"/>
<h:commandButton value="Sign Up" action="#{register.show}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>
register.java
package comp;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.faces.model.SelectItem;
import java.util.List;
@Named
@RequestScoped
public class register {
//@NotNull(message="First name cannot be null")
@Pattern(regexp="[0-9]", message="Cannot contain numbers")
private String firstName;
@NotNull(message="Last name cannot be null")
@Pattern(regexp="[0-9]", message="Cannot contain numbers")
private String lastName;
@Min(value=4, message="Password must contain at least 4 characters")
private String password;
@NotNull(message="Must enter an email address")
@Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "Email format is invalid.")
private String email;
@Pattern(regexp="[0-9]*", message="Must contain only digits")
private String phoneNumber;
@NotNull(message="Must have a gender")
private String gender;
@NotNull(message="You must know one language")
private List<SelectItem> languages;
@NotNull(message="You must have hometown")
private String hometown;
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public List<SelectItem> getGenderItems(){
return Utility.genderList();
}
public List<SelectItem> getLanguages(){
return languages;
}
public void setLanguages(List<SelectItem> languages){
this.languages = languages;
}
public List<SelectItem> getLanguageItems(){
return Utility.languageList();
}
public String getHometown(){
return hometown;
}
public void setHometown(String hometown){
this.hometown = hometown;
}
public List<SelectItem> getHometownItems(){
return Utility.hometownList();
}
public String show(){
return "showinfo";
}
}
Aucun commentaire:
Enregistrer un commentaire