views:

10

answers:

1

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());
        }
    }
}

}

+2  A: 

on-render is invoked when the page is actually being streamed back to the user. To be able to show any validation errors you will have to re-render the page, and so on-render is invoked. Can you not put those two set's inside the on-entry tag?

John V.
Yes! That worked! I've been on this all day without figuring it out (app I didn't write and webflow newbie), so this was really helpful. Thank you very much!
mranders
No problem, glad to help!
John V.