views:

852

answers:

2

In JSF, is it possible to solve the following validation / updating scenario using the validation framework of JSF (aka validators, immediate, required, etc.)?

+-- Form ----------------------------------------------------+
|                                                            |
| Field A: |____________|v|                                  |
| Field B: |____________|v|                                  |
| Field C: |____________|v| Button: [Update C using A and B] |
| Field D: |____________|v| Button: [Update D using B and C] |
|                                                            |
| More Fields: ...                                           |
|                                                            |
| Button : [Submit All]                                      |
+------------------------------------------------------------+

Where all fields are drop downs and where pressing the [Update C] button would only cause A, B and C to validate but not the rest, pressing the [Update D] button would only cause fields B, C and D to validate and pressing [Submit All] button would cause all fields to validate.

+1  A: 

Technically, yes, it is possible to do this with validators/immediate. However, it wouldn't be particularly simple.

If you add validators, the logic in [Update C using A and B] will only run if all validators pass or if its immediate attribute has been set to true. If its immediate attribute has been set to true, the button logic cannot read the submitted component values using getValue or any object the component is bound to; it must use getSubmittedValue (a bad idea).

A better approach would be to do your validation logic in a managed bean to which all the controls are bound. You can put the validation logic in whatever methods your buttons are bound to. Use the [Submit All] button to save your validated data in your database/persistence store/whatever.

McDowell
That's mainly the idea that I had. Its just that by doing that, you throw away the whole validation layer. This part of JSF sucks badly.
Loki
Validators are for ensuring the correctness of input data on a control. Use them to stop the user agent submitting malformed/out-of-range data. This is the only thing the validation API is designed to do. C'est la vie.
McDowell
The point is that sometimes you have multiple actions that can be performed on a single form and you don't even use some of the data. Having the validators activated for those fields might cause them to throw unrelated errors to the current user action.
Loki
A: 

I was usually doing similar things with Ajax, where I would submit parts that interest me to server, validate/process there, and return results to page. On the other hand, it's a complete bypass of JSF and it's validation mechanisms, so I guess it's not really the smartest / optimal solution...

Slartibartfast