Results 1 to 10 of 10

Thread: Validation through transition

  1. #1

    Default Validation through transition

    Hi,
    I want user filter the resources before assigning.Could somebody tell me if its a appropriate way doing this?

    Code:
    <view-state id="assignForm" view="..." model="myBean">
    <transition on="search" to="assignForm" bind="true">
    <evaluate ... do some form object validation before next evaluation...>
    <evaluate ... something else...>
    </transition>
    <transition on="assign" to="finish" bind="true">
    <evaluate ...>
    </transition>
    </view-state>
    Thanks for reply!

    -Jimmy

  2. #2

    Default

    You could do this since you can do anything with the evaluate statements, but there are two built in ways to do validation in Spring Web Flow 2.0. You can either make a separate validator that matches the ${model} + 'Validator' (ex: personValidator for 'person' model) or have a method embedded in your model. That should have a method name of 'validate' + ${state} (ex: 'public void validateAddressForm(MessageContext context)'). I have an example doing this posted at Spring Web Flow Subflow Webapp if you look at the PersonValidator class (@Component so registered as 'personValidator' bean) and the excerpt from excerpt from Address.

    Also look at Validating a Model section of the Spring Web Flow documentation.

  3. #3

    Default

    Hi, David

    As I known, the validator you mentioned always validates the model that view specifics when transition triggered.
    But I only want to validate some form input fileds, not every time when form submits.
    For here, assuming that the form has a Province options and a City options. City Options changes when Province options onchange event triggered, and then if user click the 'Filter resource' button, 'transition on="search"' would be signaled. So if I use a model validator, the two attributes would always failed through validation.
    Code:
    public void validateAssignForm(MessageContext context) {
    if (!StringUtils.hasText(province)) { 
    context.addMessage(new MessageBuilder().error().source("province").code("assign.form.province.error").build());
    }
    if (!StringUtils.hasText(city)) { 
    context.addMessage(new MessageBuilder().error().source("city").code("assign.form.city.error").build());
    }
    I donnt know if I missed something. Please kindly post your suggestion, thanks.

  4. #4

    Default

    So, shouldn't you be able to use the model validate method and do validateSearch and/or validateAssign? Then as you transition from assignForm to these other states each one can have it's own validation.

    Also, is there any way besides the states to check what should be validated? Like if province is blank, city shouldn't even be checked?

  5. #5

    Default

    So, I have to create various models for each divided state?

  6. #6

    Default

    No, you can have the same model with a validation method for each state. And just validate the parts you want. So you could put validateSearch and validateAssign methods in your 'myBean' model. Does that work for you?

  7. #7

    Default

    ok, i will try.
    thanks David.

  8. #8

    Default

    It still not resolved smoothly -_-
    I would prefer specific validation on each transition. Is it possible?

  9. #9
    Join Date
    Mar 2008
    Location
    Wellington, New Zealand
    Posts
    30

    Default

    You could always just stick in:
    Code:
    <evaluate expression="myBean.validateSomeStuff(messageContext)" />
    Where validateSomeStuff returns a boolean determining whether the validation has passed or not. This way you can have multiple validate methods that you call at different times.

  10. #10

    Default

    r2b2, That's exactly what I want! Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •