myfaces-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rudy De Busscher <rdebussc...@gmail.com>
Subject [ExtVal] Cross Validations and duplicate messages
Date Thu, 18 Feb 2010 07:46:12 GMT
Hi all,

There are cases that the validation message is shown twice for some Cross
Validations.

If we use for instance the Equals or NotEquals annotations where the target
component is also shown on the same page, the message is shown twice.
This is the example:

*public class Person
{
    @NotEquals(value = "lastName", validationErrorMsgKey = "First name and
last name can't have the same value")
    private String firstName;

    private String lastName;
...
}


    <h:messages />
    <h:form>
        <h:panelGrid columns="2">
            <h:outputLabel value="First name" for="firstName" />
            <h:inputText id="firstName"
value="#{personBean.selectedPerson.firstName}" label="First Name" />
            <h:outputLabel value="Last name" for="lastName" />
            <h:inputText id="lastName"
value="#{personBean.selectedPerson.lastName}" label="Last Name" />
        ....
        </h:panelGrid>
    </h:form>*

This is expected behaviour since the code wants to mark both fields as
invalid and shows the message for each field.  But when the errors are
displayed on the top of the page, the message seems to be duplicated. And in
case of the above example with a custom validation message, it can be
interpreted as a bug.

The only way, I found, to avoid the 'duplicate' message (other then creating
a custom strategy or annotation) was the creation of a
ValidationExceptionInterceptor that filters out the duplicate message.

*public class NoTargetMessageValidationExceptionInterceptor implements
        ValidationExceptionInterceptor
{
public boolean afterThrowing(UIComponent uiComponent,
            MetaDataEntry metaDataEntry, Object convertedObject,
            ValidatorException validatorException,
            ValidationStrategy validatorExceptionSource)
    {
        boolean result = true;

        FacesMessageStorage storage = getStorage(FacesMessageStorage.class,
                FacesMessageStorage.class.getName());
        for (FacesMessageHolder holder : storage.getFacesMessages())
        {
            if (holder.getFacesMessage().getDetail().equals(
                    validatorException.getFacesMessage().getDetail()))
            {
                result = false;
            }
        }
        return result;
    }
    ...
}*

This code is not specific for Cross Validation messages, but is generic.
But in most cases, a duplicate message comes from the usage of
CrossValidation annotations.

Should there be a configuration option (like a ValidationParameter) created
in version x.x.4 so that the useTargetComponentToDisplayErrorMsg outcome is
not only specified in the code but also by the annotation?

regards
Rudy

Mime
View raw message