ZK validator to check user id uniqueness on registration

I wrote a very simple validator to verify the uniqueness of user entered id by checking against database. It also validates if the user id is more than 5 characters long.

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Constraint;
import User;
import UserService;
* ZK validator for user id uniqueness.
* @author Anil
public class UserIdUniquenessValidator implements Constraint {
/** user service to pull user info from DB */
private UserService userService;
public void validate(Component comp, Object value) throws WrongValueException {
if (value == null || StringUtils.isEmpty((String) value)) {
throw new WrongValueException(comp, “Please enter User ID”);
if (((String)value).trim().length() < 4) {
throw new WrongValueException(comp, “User Id must be at least 5 characters”);
User user = userService.load((String) value);
if (user != null) {
throw new WrongValueException(comp, “User ID already exists”);
public UserService getUserService() {
return userService;
public void setUserService(UserService userService) {
this.userService = userService;
I declared this validator as spring bean in spring context file:
<bean id=”userIdUniquenessValidator” class=”com.gavs.skill.ui.validator.UserIdUniquenessValidator”>
<property name=”userService” ref=”userService”/>
ZUL file:
<bean id=”userIdUniquenessValidator” class=”UserIdUniquenessValidator”>
<property name=”userService” ref=”userService”/>
<?variable-resolver class=”org.zkoss.zkplus.spring.DelegatingVariableResolver”?>
<column label=””></column>
<column label=””></column>
<label value=”First Name” width=”150px” />
<textbox  id=”indFirstName” width=”150px” constraint=”no empty”  value=”@{user.firstName}” />
<label value=”Last Name” width=”150px” />
<textbox id=”indLastName” width=”150px” constraint=”no empty” value=”@{user.lastName}”/>
<label value=”Email” width=”150px” />
<textbox id=”indEmail” width=”150px” constraint=”/.+@.+\.[a-z]+/: Please enter an e-mail address” value=”@{user.emailId}”/>
<label value=”User Id” width=”150px” />
<textbox id=”indUserId” width=”150px” constraint=”${userIdUniquenessValidator}” value=”@{user.userId}”/>
<label value=”Password” width=”150px” />
<textbox id=”password” type=”password”  width=”150px” constraint=”no empty” value=”@{user.password}”/>
Thats all. On blur of user id field, validator validates the user entered info  and display the error message if user id already exist in database.
Posted in ZK. Tags: , . 1 Comment »

One Response to “ZK validator to check user id uniqueness on registration”

  1. SheemIcenScer Says:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: