
attractive, usable web applications
Richard's Blog - Design, coding and life in Japan
Validation methods for non forms in stripes
Submitted by Richard on Wed, 10/14/2009 - 08:22Validations methods in stripes are so handy that I wanted to use them when not validating form criteria, but when checking a login etc which could be used on multiple actions in the controller, without throwing an exception but by forwarding you to an action bean of choice and showing an error message. I used a message instead of an error as errors do seem to be bound more to forms even global errors. Errors were also unavailable on a redirect resolution (but available to a forward resolution to a jsp - but I didn't want that).
First override the getSourcePageResolution method in a custom context including a setter.
public class MyActionBeanContext extends ActionBeanContext {
private Resolution sourcePageResolution;
@Override
public Resolution getSourcePageResolution() {
if (sourcePageResolution != null)
return sourcePageResolution;
return super.getSourcePageResolution();
}
public void setSourcePageResolution(Resolution sourcePageResolution) {
this.sourcePageResolution = sourcePageResolution;
}
}
Then you can create a method in your base action bean which will add a dummy error - this gets expired when using a redirect resolution to a action bean. I create a message and tell it that the source of the request is the redirect action bean of choice.
public abstract class BaseActionBean implements ActionBean {
@SuppressWarnings("unchecked")
protected void setMessageRedirect(Class redirect, String message, ValidationErrors errors){
errors.addGlobalError(new SimpleError("Dummy Error"));
setMessage(message);
getContext().setSourcePageResolution(
new RedirectResolution(redirect).addParameter("forward", getCreatedUrl())
);
}
}
I can then call it simply as follows
@ValidationMethod
public void validateUser(ValidationErrors errors) {
if(getLoginDMember()==null){
setMessageRedirect(LoginActionBean.class, "loginNeeded", errors);
}
}
Tags:
Recent Blog Posts
- Why you should use Rails 3 and not Django 5th Sep 10, 12:28
- Doctrine 2 and why you should use it! 29th Jul 10, 15:23
- How to localize your main blog listing page using Drupal 26th Jun 10, 11:46
- Creating Rails like template helpers in Django 24th Jun 10, 22:01
- Error: No module named messagesdjango.contrib.contenttypes 22nd Jun 10, 22:00
Post new comment