Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top