Announcement Announcement Module
No announcement yet.
How do I properly invalidate on Model Validation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I properly invalidate on Model Validation


    I'm trying to add model validation to invalidate a transition in the webflow; however, it seems that it always passes validation and completes the transaction even when it shouldn't. I've had this working similarly in older projects (using 2.0.3, I'm currently trying it in 2.0.8)

    Here's the important parts of the code:

    my flow xml:
    <flow xmlns=""
        <secured attributes="ROLE_USER" />
        <var name="viewUsers" class=""/>
        <view-state id="viewUsers" view="viewUsers.xhtml" model="viewUsers">
                <evaluate expression="viewUsers.updateUsers()" />
            <transition on="edit" to="editUser" bind="true" validate="true"/>
        <end-state id="editUser">
                <evaluate expression="viewUsers.commit()"/>
    my transition trigger button a save button in viewUsers.xhtml
    (view users has a modal in it with a dialog for editing user info, which i wish to validate. We're also using richfaces here.)
    <a4j:commandButton value="#{uim.button_save}" rendered="#{viewUsers.editModal}" action="edit" reRender="table" />

    and this simple version of my model bean "ViewUsers" validation function:
    (I've removed all real boolean checks because I'd like to see this work first.)
     public boolean validateViewUsers(MessageContext context) {
            log.debug("Validating the modification of a user, after the save button is pressed.");
            return false;
    I noticed that the webflow documentation has changed to use a validationContext; but I also noticed that the return type is now void. So how does spring determine if it fails my validation? Is it by the presence of an Error Message in the MessageContext? If so, I have also tried assigning a message to the context and it continues to proceed, as if my validation passes.
    I know it completes the transaction instead of rolling back because the commit function in my end-state wrote a log after my validation function wrote a log.
    I'd like to maintain model validation instead of creating a custom validator, because the validation here is very specific, and the way we've organized this in the past has been very clean.

    Any insight as to how this works is much appreciated,

  • #2
    When you're using JSF, I don't think you need (or should) be using the model attribute of the view-state, or the bind and validate attributes of transitions. After all, JSF takes care of the binding for you, and you can always invoke the validation yourself.

    With JSF and SWF, the usage I tend to see is to simply use your beans as you normally would, not declaring a model for the view-state. For transitions which require validation, you can add an evaluate action inside the transition to call your (custom) validate method. If it returns false, then the transition (and any following actions in the transition) is cancelled.


    • #3
      thanks InverseFalcon,

      I should have mentioned we are using facelets and we are particularly interested in using the SWF validation which allows us to simply handle unique validation scenarios easily. Since we have this strategy working in other projects... I believe something unique to this project is causing issue.

      It is my understanding that bind="true" validate="true" in the transition tag are default true, so binding in validation is expected to happen normally upon a transition in web flows.

      I've been doing validation this way since since webflows 1.0, so i'm not familiar with using action-states to validate, it doesn't seem like this technique would allow me to return messages in the MessageContext? (error messages to the screen, which I intend to do) and I don't see anything in SWF chapter 5 related to this. Could you elaborate?



      • #4
        I think the bind and validation attributes on transitions (as well as the model attribute for the view-state) are mostly there to support SWF with Spring MVC. When you're using JSF, JSF handles the binding for you, thus I don't think there's much need for a model for the view. The validation (example below) can be done manually, and provides more flexibility than simply having a "validate" attribute in the transition. For example, you could evaluate multiple beans, or do a validation and check some additional value as well.

        We're not using action-states for this, just evaluate actions, which can be used in many places, including transitions. When an evaluate action is used in a transition, if its outcome is false, the transition it is a part of is cancelled. For transitions requiring validation, I typically do something like this:

        <view-state id="myView">
          <transition on="someSubmitEvent" to="someState">
            <evaluate expression="myFormOrBean.validateMethod(messageContext)"/>
            <!-- If above evaluation fails, transition is cancelled, and any additional actions (set, evaluate, render) are not executed -->
        Last edited by InverseFalcon; Feb 24th, 2010, 02:19 PM.


        • #5
          Ah, I didn't think to take that approach. I guess I should then explicitly tell it not to validate. Or else it may attempt to do so twice (even though it may or may not find something).
          I suppose this works (which it does, I checked), But I'm still interested as to why the documented approach for validation (the first half here: isn't working in this case.
          Thanks, we'll probably use this strategy in the mean time until we understand how to use the spring initiated validation more correctly, if you have more insight to how that works I'd like to here it.


          • #6
            Unfortunately, I don't have much insight into that type of validation. Although it's not in the documentation anywhere, my guess (as mentioned earlier) is that this isn't applicable to JSF.

            There is a section on the type of validation I described above in the Handling JSF Events With Spring Web Flow section of the documentation. See the Performing Model Validation section.