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

  • How do I properly invalidate on Model Validation

    Hi,

    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:
    Code:
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
    
        <secured attributes="ROLE_USER" />
        <var name="viewUsers" class="com.nid.um.webflows.ViewUsers"/>
    
        <view-state id="viewUsers" view="viewUsers.xhtml" model="viewUsers">
            <on-entry>
                <evaluate expression="viewUsers.updateUsers()" />
            </on-entry>
            <transition on="edit" to="editUser" bind="true" validate="true"/>
        </view-state>
    
        <end-state id="editUser">
            <on-entry>
                <evaluate expression="viewUsers.commit()"/>
            </on-entry>
        </end-state>
    </flow>
    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.)
    Code:
    <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.)
    Code:
     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,
    Thanks

  • #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.

    Comment


    • #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?

      thanks,

      Comment


      • #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:

        Code:
        <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 -->
          </transition>
        </view-state>
        Last edited by InverseFalcon; Feb 24th, 2010, 02:19 PM.

        Comment


        • #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: http://static.springsource.org/sprin...l/ch04s10.html) 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.

          Comment


          • #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.

            Comment

            Working...
            X