I have a Spring WebFlow app that causes me grey hairs. What I'm trying to accomplish if validation succeeds is to simply show the same view with different data. This works. However, when invalid data are entered, I want to show the previous view, but with the erroneous values pre-filled and the error messages telling my user how to fix the error above. However, regardless of whether the validation succeeds or not, the app still does the stuff inside on-render
. Since the values are invalid, it crashes my app. How can I do the on-render
stuff only when validation succeeds?
My view-state config looks a little bit like this:
<view-state id="mine" model="myModel">
<var name="myModel" class="com.examplelMyModel" />
<on-entry>
<set name="foo" value="bar" />
</on-entry>
<on-render>
<set name="requestScope.stuff" value="stuffService.loadStuff( ... )" />
<set name="viewScope.otherStuff" value="otherStuffService.loadOtherStuff( requestScope.stuff, myModel.a, myModel.b )" />
</on-render>
<transition on="reloadMine" validate="true"/>
</view-state>
My validator looks like this:
public class MyModelValidator{
...
public void validateMine(MyModel myModel, ValidationContext context) {
Locale currentLocale = LocaleContextHolder.getLocale();
MessageContext messages = context.getMessageContext();
DateTime a= new DateTime(myModel.getA());
DateTime a = new DateTime(myModel.getB());
DateTime now = new DateTime();
if(a.isAfter(now) || b.isAfter(now)){
messages.addMessage(new MessageBuilder().error().defaultText(messageSource.getMessage("ohno", null, currentLocale)).build());
}
}
}
}