Sunday, September 03, 2006

Vlad the Impaler, DWR, Struts, Validation and The Beatles

Now open source software is a very splendid thing. Sometimes however, when things don't work quite as you expected or you go off piste slightly you can find your self alone. Now this is not just regular alone of course but really really alone, worse than being left behind on the moon when the spaceship leaves without you.

What I am talking about of course is searching an open source software mailing list archive (in my case Commones Validator) and getting no search results back. when this happens you are really alone.

The next most frustrating thing is when you search the mailing list archive and you get one result back from a bloke in Serbia called Vlad. Vlad has the same problem as you but no-one has replied, worse than that his post was submitted at the sametime as the Beatles "Please Please Me" went to number one.

What I had been trying to do was getting the Apache Commons Validator framework to work with my DWR based AJAX code.

What is good about this is that you can add some @attributes above soome getters in you model code, ala:


/**
* @return Returns the qTxt.
* @hibernate.property
* @struts.form-field
* @struts.validator type="required"
* @struts.validator type="maxlength" arg1value="100"
* @struts.validator-var name="maxlength" value="100" arg1value="100"

*/
public String getQTxt() {
return qTxt;
}


from this you can generate via Xdoclet all validation code like required and maximum length.

This all works nicley if you are on the straight and narrow and are just using JSPs and Struts. If like me you need to to run the validator from Java code because you have gone AJAX crazy then sadly you end up having a night out with Vlad.

So in the spirit of the caring, sharing internet here is my validation code, it's a bit shonky but it does work, so far. Basically you pass in the name of the form and the bean you want to validate and the function populates an array of erro messages.



/**
* @param formName
* @param bean
* @param errorMessages
* runs the stuts validator on a form.
*/
private void validateForm(String formName, Object bean,
ArrayList errorMessages) {

ValidatorResources resources = getResources();
MessageResources messageResources = MessageResources
.getMessageResources(Constants.BUNDLE_KEY);

// set up the validator
Validator validator = new Validator(resources, formName);
validator.setParameter(Validator.BEAN_PARAM, bean);
validator.setOnlyReturnErrors(true);

// run the validator
ValidatorResults results = null;
try {
results = validator.validate();
} catch (ValidatorException ex) {

log.error("Exeception in in validator:" + ex.toString());

}

// iterate over list and build error messages
Iterator itr = results.getPropertyNames().iterator();
Form form = resources.getForm(Locale.getDefault(), formName);

while (itr.hasNext()) {
String name = (String) itr.next();

Field field = form.getField(name);

ValidatorResult result = results.getValidatorResult(name);

String prettyName = messageResources.getMessage(formName + "."
+ name);

Object[] argVals = new Object[2];

argVals[0] = prettyName;

Map actionMap = result.getActionMap();
Iterator keys = actionMap.keySet().iterator();

while (keys.hasNext()) {
String actName = (String) keys.next();
ValidatorAction action = resources.getValidatorAction(actName);

argVals[1] = field.getVarValue(action.getName());

errorMessages.add(messageResources.getMessage(action.getMsg(),
argVals));

}
}

}

0 Comments:

Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?