
front and back-end web development, Leeds, UK
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
- Testing controllers in Lithium 2nd Feb 12, 18:19
- Practical Internationalization in Lithium 31st Dec 11, 02:06
- Using OAuth in Lithium 30th Dec 11, 23:47
- How to add your own Tokens from CCK fields in Druapl 7 17th Jun 11, 04:49
- Weaving Lithium #li3 into a legacy PHP application incrementally 5th Oct 10, 11:54