Saturday, May 19, 2007

jgoodies validation (very easy!!!)

for last couple of days i was looking into jgoodies validation. but i found that it doesn't have enough quick start guide or simple sample code. the sample code provided with jgoodies are too complex. i have searched the net but i failed.
then i asked hasan's help. he said, "give me the complex code, i will simplify it". then i think, why don't i do that. so here is my simple code.

first assume you have a domain object as follow:

public interface User {
public String getUserName();
public void setUserName(final String pUserName);
public String getPassword();
public void setPassword(final String pPassword);
}
and it's implementation UserImpl.

you also have a form with two text fields mUserNameTextField and mPasswordField

now first create a Validator class
public class UserValidator extends AbstractValueModel
implements Validator, User {
private User mUser = new UserImpl();
public Object getValue() {
return this;
}
public void setValue(Object pObject) {
// never used
}
public User getUser() {
return mUser;
}
public void setUser(final User pUser) {
mUser = pUser;
}
public ValidationResult validate() {
ValidationResult result = new ValidationResult();
if (ValidationUtils.isEmpty(mUser.getUserName())) {
result.addError("User name is required");
}
if (ValidationUtils.isEmpty(mUser.getPassword())) {
result.addError("Password is required");
}
return result;
}
public String getUserName() {
return mUser.getUserName();
}
public void setUserName(final String pUserName) {
mUser.setUserName(pUserName);
}
public String getPassword() {
return mUser.getPassword();
}
public void setPassword(final String pPassword) {
mUser.setPassword(pPassword);
}
}
in the form class create there fields
private ValidationResultModel mValidationResultModel =
new DefaultValidationResultModel();
private PresentationModel mPresentationModel =
new PresentationModel(new UserValidator());
private JLabel mMessageLabel = ValidationResultViewFactory.
createReportIconAndTextLabel(mValidationResultModel);
after initializing the form
Bindings.bind(userNameTextField,
mPresentationModel.getBufferedModel("userName"));
Bindings.bind(passwordTextField,
mPresentationModel.getBufferedModel("password"));
here "userName" and "password" are the properties of your domain class.

finally in the action handler for form submit
mPresentationModel.triggerCommit();
UserValidator validator =
(UserValidator) mPresentationModel.getBean();
ValidationResult result = validator.validate();
mValidationResultModel.setResult(result);
if (result.isEmpty()) {
// do your action handling here
}
isn't it very easy!!!

4 comments:

Uditha said...

Thanks for ur effort to make these things simple. keep it up.

Regards!

- Uditha

Unknown said...

There is now a simplified example in the jgoodies validation tutorials (SimpleDomainValidationExample) which validates on commit. I found it fairly intuitive to follow if you are familiar with the style of the Swing Application Framework (JSR296).

Samuel Ventura said...

Just posted a self contained sample here.

index said...

what have in the class 'UserImpl'
dont understand