How can I make WebFlow abort the transitioning and just view the previous page when validation fails?

StackOverflow https://stackoverflow.com/questions/4024468

  •  26-09-2019
  •  | 
  •  

Question

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

}

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top