Skip to content

Instantly share code, notes, and snippets.

@edwerner
Last active January 3, 2016 18:39
Show Gist options
  • Save edwerner/8503242 to your computer and use it in GitHub Desktop.
Save edwerner/8503242 to your computer and use it in GitHub Desktop.
Contact POJO with javax and Hibernate annotations with some handy regex constraints.
package com.contact.form.app.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.SafeHtml;
// contact domain object
/**
* The Class Contact.
*/
@Entity
@Table(name="contact")
public class Contact {
/** The id. */
@Id
@GeneratedValue
private long id;
/** The first name with hibernate validation constraints. */
@NotBlank(message = "First name cannot be blank")
@SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE, message = "No HTML/JavaScript allowed")
@Pattern(regexp = "^(?!.*[`~$&+.,:;=?@#|/'<>^*(){}%!\"\\[\\]-]).*$", message = "No special characters allowed")
@Size(max = 75, message = "Cannot be longer than {max} characters")
private String firstName;
/** The last name with hibernate validation constraints. */
@NotBlank(message = "Last name cannot be blank")
@SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE, message = "No HTML/JavaScript allowed")
@Pattern(regexp = "^(?!.*[`~$&+.,:;=?@#|/'<>^*(){}%!\"\\[\\]-]).*$", message = "No special characters allowed")
@Size(max = 75, message = "Cannot be longer than {max} characters")
private String lastName;
/** The email address with hibernate validation constraints. */
@NotBlank(message = "Email address cannot be blank")
@Email(message="Please provide a valid email address")
@SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE, message = "No HTML/JavaScript allowed")
@Size(max = 50, message = "Cannot be longer than {max} characters")
private String emailAddress;
/** The phone number with hibernate validation constraints. */
@NotBlank(message = "Phone number cannot be blank")
@SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE, message = "No HTML/JavaScript allowed")
@Pattern(regexp = "\\d{3}-\\d{3}-\\d{4}", message = "Please enter a valid phone number: XXX-XXX-XXXX")
private String phoneNumber;
/** The message with hibernate validation constraints. */
@SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE, message = "No HTML/JavaScript allowed")
@Size(max = 500, message = "Message cannot be longer than {max} characters")
private String message;
/** The model with hibernate validation constraint. */
@NotEmpty(message = "Please choose a model")
private String model;
/**
* Instantiates a new contact.
*/
public Contact() {
// empty constructor
}
/**
* Gets the id.
*
* @return the id
*/
public long getId() {
return id;
}
/**
* Sets the id.
*
* @param id the new id
*/
public void setId(long id) {
this.id = id;
}
/**
* Gets the first name.
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the first name.
*
* @param firstName the new first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the last name.
*
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets the last name.
*
* @param lastName the new last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets the email address.
*
* @return the email address
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* Sets the email address.
*
* @param emailAddress the new email address
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
/**
* Gets the phone number.
*
* @return the phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Sets the phone number.
*
* @param phoneNumber the new phone number
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Gets the message.
*
* @return the message
*/
public String getMessage() {
return message;
}
/**
* Sets the message.
*
* @param message the new message
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Gets the model.
*
* @return the model
*/
public String getModel() {
return model;
}
/**
* Sets the model.
*
* @param model the new model
*/
public void setModel(String model) {
this.model = model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment