Requirement Constraints

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 10 July 2013

ZK MVVM Form Binding CRUD with Spring and Hibernate - Part 13

Posted on 02:55 by Unknown

More & More Validation using Hibernate Validator

In the previous article 12, We applied some validation constraints to our UserProfile class. Now we will add more constraints to tighten the user inputs as per some business logic.
 

The following constraints are available in the JSR-303 API

  • AssertFalse: The element must be false
  • AssertTrue: The element must be true
  • DecimalMax: The element must be a number and lower or equal to the specified value
  • DecimalMin: The element must be a number and greater or equal to the specified value
  • Digits: Must be a number within an accepted range
  • Future: Must be a date in the future
  • Max: Must be a number with a value lower or equal to the specified maximum
  • Min: Must be a number with a value greater or equal to the specified minimum
  • NotNull: Must not be null
  • Null: Must be null
  • Past: Must be a past date
  • Pattern: Must match a defined regular expression
  • Size: Must match defined boundaries. Valid for strings, collections, maps and arrays



Step 1:
We will add more constraints as follows

package zkexample.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.annotations.NamedQuery;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

@Entity
@Table(name = "userprofile")
@NamedQuery(name = "UserProfile.findUserByUserID", query = "SELECT usr FROM UserProfile as usr WHERE usr.userLoginID = ?")
public class UserProfile implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotBlank(message = "First Name cannot be empty")
@Size(min = 2, message = "First name is too small")
private String firstName;

@NotBlank(message = "Last Name cannot be empty")
@Length(min = 2, max = 50, message = "LastName should be 2 to 50 Characters size")
private String lastName;

private String middleName;

@NotBlank(message = "User Account Number cannot be empty")
@Pattern(regexp = "^[a-zA-Z]{2}-\\d+$", message = "Invalid Account Number Number. First two letter should be Alphabets and then one hyphen and then any digits. For example AA-333, BB-44")
private String userAccountNumber;

@Pattern(regexp = "[0-9]{3}-[0-9]{2}-[0-9]{4}", message = "Invalid SSN format")
private String SSN;

@NotBlank(message = "Address1 cannot be empty")
private String address1;

private String address2;

@NotBlank(message = "City cannot be empty")
private String city;

@NotBlank(message = "State cannot be empty")
private String State;

@NotBlank(message = "ZipCode cannot be empty")
private String zipCode;

@NotBlank(message = "Email cannot be empty")
@org.hibernate.validator.constraints.Email(message = "Invalid Email Format")
private String email;

@NotBlank(message = "Login Name cannot be empty")
private String userLoginID;

@NotBlank(message = "Password cannot be empty")
private String password;

private Integer system;

@NotBlank(message = "Theme cannot be empty")
private String theme;

@Column(name = "userPhoto")
@Lob
private byte[] userPhoto;

@Temporal(TemporalType.DATE)
@Past(message = "Date of birth should be past date")
private Date DOB;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getMiddleName() {
return middleName;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

public String getUserAccountNumber() {
return userAccountNumber;
}

public void setUserAccountNumber(String userAccountNumber) {
this.userAccountNumber = userAccountNumber;
}

public Date getDOB() {
return DOB;
}

public void setDOB(Date dOB) {
DOB = dOB;
}

public String getSSN() {
return SSN;
}

public void setSSN(String sSN) {
SSN = sSN;
}

public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}

public String getAddress2() {
return address2;
}

public void setAddress2(String address2) {
this.address2 = address2;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return State;
}

public void setState(String state) {
State = state;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getUserLoginID() {
return userLoginID;
}

public void setUserLoginID(String userLoginID) {
this.userLoginID = userLoginID;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getSystem() {
return system;
}

public void setSystem(Integer system) {
this.system = system;
}

public String getTheme() {
return theme;
}

public void setTheme(String theme) {
this.theme = theme;
}

public byte[] getUserPhoto() {
return userPhoto;
}

public void setUserPhoto(byte[] userPhoto) {
this.userPhoto = userPhoto;
}

}


image


Step 2:
We can also create our own custom constraints to validate our fields. For simple example of creating own custom constraints, please look this
post.

Now we will see how we can do cross field validation. For example, in the standard User registration form, there will be two fields for password; password and confirm password. Before submit, it make sense to validate that both the fields should contains the same value. Now we will implement the same in our project

First Let us create the validator Class. Under the package “utilities”, create a class called “FieldMatchValidator” as follows”


package zkexample.utilities;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.beanutils.BeanUtils;

public class FieldMatchValidator implements
ConstraintValidator<FieldMatch, Object> {

private String firstFieldName;
private String secondFieldName;

@Override
public void initialize(final FieldMatch constraintAnnotation) {
firstFieldName = constraintAnnotation.first();
secondFieldName = constraintAnnotation.second();
}

@Override
public boolean isValid(final Object value,
final ConstraintValidatorContext context) {
try {
final Object firstObj = BeanUtils
.getProperty(value, firstFieldName);
final Object secondObj = BeanUtils.getProperty(value,
secondFieldName);
return firstObj == null && secondObj == null || firstObj != null
&& firstObj.equals(secondObj);
} catch (final Exception ignore) {

}
return true;
}

}

Next we need to create our annotation based on the above validator class. Create an interface called “FieldMatch” under the package “utilities” as follows

package zkexample.utilities;


import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;

/**
* * Validation annotation to validate that 2 fields have the same value. * An
* array of fields and their matching confirmation fields can be supplied. * *
* Example, compare 1 pair of fields: * @FieldMatch(first = "password", second =
* "confirmPassword", message = "The password fields must match") * * Example,
* compare more than 1 pair of fields: * @FieldMatch.List({ * @FieldMatch(first =
* "password", second = "confirmPassword", message =
* "The password fields must match"), * @FieldMatch(first = "email", second =
* "confirmEmail", message = "The email fields must match")})
*/
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {
String message() default "{constraints.fieldmatch}";

Class[] groups() default {};

Class[] payload() default {};

/** * @return The first field */
String first();

/** * @return The second field */
String second();

/**
* * Defines several @FieldMatch annotations on the same element * * @see
* FieldMatch
*/
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
@interface List {
FieldMatch[] value();
}
}


image

Now we are ready to use our own custom validator in the POJO Class.

Step 3:
Next we will modify our UserProfile.java to use the above validator. Before that, we need to add one more property with @Transient annotation as follows

@Transient
private String confirmPassword;

public String getConfirmPassword()
{
return confirmPassword;
}

public void setConfirmPassword(String confirmPassword)
{
this.confirmPassword = confirmPassword;
}


You can read more about @Transient annotation here.  Now we will add the above custom validation in the top of the class as follows

@Entity
@Table(name = "userprofile")
@NamedQuery(name = "UserProfile.findUserByUserID", query = "SELECT usr FROM UserProfile as usr WHERE usr.userLoginID = ?")
@FieldMatch.List({ @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"), })
public class UserProfile implements Serializable {


Step 4:
Next we need to change our UI to add this new field Confirm Password. Open UserCRUD.zul  and change the bottom part as follows

	<panel width="95%" sclass="sectionPanel">
<panelchildren>

<grid sclass="vgrid">
<columns>
<column width="20%"></column>
<column width="20%"></column>
<column width="20%"></column>
<column width="20%"></column>
</columns>
<rows>
<row>
<vlayout>
<hlayout>
<label value="Login ID"
sclass="flabel" />
<label value="*"
sclass="flblreq" />
</hlayout>
<textbox id="loginid" hflex="1"
readonly="@load(vm.makeAsReadOnly)" mold="rounded"
value="@bind(fx.userLoginID)" />
</vlayout>
<vlayout>
<hlayout>
<label value="Password"
sclass="flabel" />
<label value="*"
sclass="flblreq" />
</hlayout>
<textbox id="password" hflex="1"
readonly="@load(vm.makeAsReadOnly)" mold="rounded"
value="@bind(fx.password)" />
</vlayout>
<vlayout>
<hlayout>
<label value="Confirm Password"
sclass="flabel" />
<label value="*"
sclass="flblreq" />
</hlayout>
<textbox id="confirmpassword" hflex="1"
readonly="@load(vm.makeAsReadOnly)" mold="rounded"
value="@bind(fx.confirmPassword)" />
</vlayout>
<vlayout>
<hlayout>
<label value="Theme"
sclass="flabel" />
<label value="*"
sclass="flblreq" />
</hlayout>
<combobox model="@load(vm.themes)" hflex="1"
width="30%" mold="rounded" readonly="@load(vm.makeAsReadOnly)"
selectedItem="@bind(fx.theme)" value="@bind(fx.theme)" />
</vlayout>
</row>
</rows>
</grid>
</panelchildren>
</panel>


That’s all. Now you can run and see how all our validation part works.

You can download the source here.
Don’t forget to check online demo here. Use username as wing and password as wing
Resources
http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303
http://cloverink.net/cross-field-validation-with-hibernate-validator-jsr-303/
http://emrpms.blogspot.in/2012/06/annotate-non-persistent-properties-with.html
http://emrpms.blogspot.in/2012/08/hibernate-validator-examples.html

Read More
Posted in | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • ZK Example for inline Editing with Add New and Delete
    I am quite impressed on this demo from ZK . But adding new record and delete existing record is missing as part of typical CRUD. So i thoug...
  • EDI 5010 Documentation 837 Professional - Loop 2010BB Payer Name
    2010BB Payer Name          In this loop, all the information will be taken from Insurance master screen. Take a look of our sample screen...
  • EDI 5010 Documentation–837 - BHT - Beginning of Hierarchical Transaction
    BHT – Beginning of Hierarchical Transaction Loop Seg ID Segment Name Format Length Ref# Req Value   BHT Beginning of Hier...
  • Hibernate Validator Example 2
    In this example, we will see some more validation constraints such as @email, @past, @length, etc. And also we will also define custom error...
  • ZK Passing Parameter between two files using MVVM–Part 1
    Overview This is the first series of articles about Passing parameter between two zul files using MVVM Design pattern .This article will fo...
  • MVVM Command annotation and Notify change example
    Here is an example, how to pass parameter on a zul through MVVM Command binding annotation. ZK URL http://books.zkoss.org/wiki/ZK%20Develo...
  • History of Present Illness
    HPI - One of the main component of Clinical History. What is an HPI ? The history of present illness (HPI) is a chronological description...
  • Patient Demographics
    Patient browse (search) is the key element for any EMR / PMS Software. In my past 15 years experience, i involved more than 5 times in desig...
  • ViewModel Class Java Annotation @Init, @NotifyChange, @Command
    In following sections we'll list all syntaxes that can be used in implementing a ViewModel and applying ZK bind annotation. The ZK binde...
  • Good Website Design Links
    Form Design Label Placement in Forms International Address Fields in Web Forms 40 Eye-Catching Registration Pages blog-comment-form-...

Categories

  • Billing Process
  • C Workbook
  • C++ Workbook
  • Eclipse Tips
  • EDI 5010
  • EMR Appointment Features
  • EMR Labs Stuff
  • EMR PMS Links
  • EMR Use cases
  • EMR Vital Sign
  • Good Website Design
  • Hibernate Criteria Queries
  • Hibernate Introduction
  • Hibernate Introduction Setup
  • Hibernate Mapping
  • Hibernate POC
  • Hibernate Validator
  • Hibernate–Java Environment setup
  • HPI
  • Java
  • Maven
  • MU Certification
  • NPI
  • PQRS
  • Practice Management System
  • Spring Security
  • Tech Links
  • Today Tech Stuff
  • zk
  • ZK Hibernate
  • ZK 5 Databinding
  • ZK Application
  • ZK Calling Another ZUL
  • ZK CheckBox
  • ZK CreateComponents
  • ZK CSS
  • ZK extended Components
  • ZK Foreach
  • ZK Forum Posts
  • ZK Framework
  • ZK Hibernate Setup
  • ZK ID Space
  • ZK Include
  • ZK Installation
  • ZK iReport
  • ZK Layout
  • ZK Listitem Pagination
  • ZK Message Box
  • ZK MVC
  • ZK MVC Combox Box
  • ZK MVC CRUD Examples
  • ZK MVC Listbox
  • ZK MVVM
  • ZK MVVM Combo
  • ZK MVVM CRUD
  • ZK MVVM ListBox
  • ZK Spring
  • ZK TextBox

Blog Archive

  • ▼  2013 (105)
    • ►  December (3)
    • ►  September (7)
    • ►  August (13)
    • ▼  July (1)
      • ZK MVVM Form Binding CRUD with Spring and Hibernat...
    • ►  June (11)
    • ►  May (3)
    • ►  April (14)
    • ►  March (19)
    • ►  February (21)
    • ►  January (13)
  • ►  2012 (177)
    • ►  December (1)
    • ►  November (13)
    • ►  October (19)
    • ►  September (24)
    • ►  August (26)
    • ►  July (6)
    • ►  June (37)
    • ►  May (30)
    • ►  April (16)
    • ►  March (1)
    • ►  January (4)
  • ►  2011 (5)
    • ►  December (1)
    • ►  November (1)
    • ►  July (1)
    • ►  June (1)
    • ►  April (1)
  • ►  2010 (1)
    • ►  September (1)
Powered by Blogger.

About Me

Unknown
View my complete profile