PDA

View Full Version : Partial/Conditional Validation



dromoli
Jun 5th, 2008, 08:13 AM
Hi everybody,

I am working with SWF 2.0 and Spring MVC and I'm facing a problem when I have to conditionally validate a subset of fields of my model object depending on the transition that triggered validation. That is, if the user clicked on button A, then transition T1 is triggered and a particular subset of the model object fields have to be validated. But if the user clicked on button B, then T2 is triggered and then I have to validate a different subset of fields.

I am aware of 3 ways of performing programmatic validation, namely:

1) Implementing org.springframework.validation.Validator

public void validate(Object obj, Errors errors)

2) Implementing a model validate method

public void validate${state}(MessageContext context)

3) Create a class that defines a public method with the name validate${state}, where state is the id of your view-state

public void validate${state}(yourModelObject, MessageContext context)


The problem is that none of these 3 ways let me figure out which transition triggered the validation and thus I am not able to determine which subset of fields I have to validate.

How can I determine from inside the validator code which fields to validate?

Any help will be much appreciated.

Keith Donald
Jun 5th, 2008, 08:28 AM
Interesting use case. Yes, this is currently not supported, though we can certainly support it by allowing the id of the user event that occurred to be an argument to the validation method. Would you mind creating a New Feature JIRA to track this? It's a minor new feature, so we can look at introducing it in a maintenance revision (most likely 2.0.3, as 2.0.2 is about to go out).

Keith

fanfy
Jun 5th, 2008, 08:32 AM
I think you could let swf do the bind and implement your validate methods in a separate bean with whatever name you want. Then in every transition you call manually the proper validate method with <evaluate .../>.

Keith Donald
Jun 5th, 2008, 08:51 AM
Yes, but he wouldn't be able to use the validation invocation-by-convention features, then; in conjunction with the "model" attribute.

Keith

dromoli
Jun 5th, 2008, 08:55 AM
Just created the JIRA request.

Best regards,
Diego

Loumeister
Jun 5th, 2008, 10:06 AM
How can I determine from inside the validator code which fields to validate?

Any help will be much appreciated.

I have the same requirement and what I do is set a status in the object requiring validation (even if only transient), and then construct a CASE statement based on that status value to validate the fields as such.

This is nice because it is decoupled from the VIEW layer so I can reuse the logic for my service layer.

HTH,

Keith Donald
Jun 5th, 2008, 11:18 AM
Lou,

Interesting approach. How do have the status value set exactly?