Hi,
I have never really felt good about the validation framework of JSF (and
Struts for that matter). The problem is that you are forced to put
validation logic into your view layer, which in the case of JSF means
polluting your jsp with validator tags.
This becomes especially problematic when you have more than one GUI for
your application, because then you have to duplicate the validation logic.
What I would prefer is to be able to do validations JavaBeans-style,
meaning I want to put my validation logic inside the setters of the
model classes, throwing PropertyVetoExceptions when the validation
fails. That way, your model classes will never be in an inconsistent state.
I have been working on way to do this in MyFaces. It involves
registering a custom PropertyResolver (thanks to Jan Dockx for this hint
:-) ) and a small change to the UIInput class.
The custom PropertyResolver checks if the thrown Exception is a
PropertyVetoException, and then wraps it in an EvaluationException, so
as to be JSF spec compliant:
} catch (Exception e) {
if (e instanceof PropertyVetoException)
{
PropertyVetoException pve = (PropertyVetoException) e;
throw new EvaluationException(pve.getMessage(), pve);
} else {
throw new EvaluationException("Exception setting value of index "
+ index + " of bean " + base.getClass().getName(), e);
}
}
The UIInput needs a change in the updateModel method. In the current myfaces
implementation
the UIInput catches all runtime exceptions and adds an error message with key
CONVERSION_MESSAGE_ID
to the FacesContext.
In order for my PropertyVetoException message to be displayed, I have to check
if the original
cause of the RuntimeException happens to be a PropertyVetoException, in which
case the FacesMessage
contains the message of the PropertyVetoException.
I don't think that this change would compromise the JSF spec compliance of
myfaces, and the default
behaviour would not change unless you register that custom PropertyResolver.
Therefore I would like
to suggest applying this change to the myfaces sourcetree.
What do you think?
Kind regards,
Jurgen