2

When validation fails for a component, I am trying to do is the following:

  • Change the border of inputs that has not failed validation to green
  • Change the border of inputs that has failed validation to red
  • On the top of the page, render a list that has internationalized label/message pairs
  • Show only error messages

What I have so far:

I use Bean validation:

@Size(min = 5, max = 20)

I manage the colouring with JS/JQuery with the following:

  • I render a hidden field with validation status:

styleClass="hidden #{facesContext.validationFailed?'failed-validation':'success-validation'}"

  • On the components to be validated I set the individual validation status:

#{component.valid?'input-valid-tobe':'input-error'}"

  • On my Ajax action I register a onevent="xxx" function that replaces the CSS with the correct valid ones.i.e. I only want to highlight the validated ones if validation has failed:
function xxx(data){
  if(data.status == "success"){
      if ($('.failed-validation')[0]){
          $('.input-valid-tobe').addClass('input-valid');
      }
  }
}

Now to the problem, rendering the messages: I would like to render a list that shows a label and a message for the component.

Messages:

I suppose I could use bean validation I18N as describe here for the messages:

How does it work with bean validation messages and i18n in JSF2? http://jcp.org/aboutJava/communityprocess/edr/jsr303/index.html

..But, I was hoping that maybe there is something new in JSF 2.2! Is there?

Labels:

Shouldn't <h:messages/> render the labels for the input fields? (It doesn't for some reason)

Filtering: Is there a way to filter the messages by severity?

(other than creating a custom bean of using CSS to hide messages that I don't want to show)

Community
  • 1
  • 1
Ioannis Deligiannis
  • 2,679
  • 5
  • 25
  • 48

1 Answers1

2

You can customize and localize them via ValidationMessages_xx_XX.properties files.

E.g.

  • ValidationMessages.properties - default locale (usually American English, en_US)
  • ValidationMessages_es.properties - Spanish language
  • ValidationMessages_en_GB.properties - British English language

If an entry is not found in a locale-specific bundle, a fallback is made to default bundle.


As to labels, they are by default indeed not added. You can easily tell JSF to prefix bean validation messages with the JSF label by the following entry in the properties file as identified by <message-bundle> of faces-config.xml:

javax.faces.validator.BeanValidator.MESSAGE = {1}: {0}

The {0} is the message of the bean validator and the {1} is the label of the JSF component. This thus end up like:

Label of component: bean validation message

If you want more fine grained control of positioning of the label, you'd need to create a custom bean validation message interpolator. JSF utility library OmniFaces has such one.


As to filtering, there's no builtin way by <h:messages>. You'd need to iterate over the messages yourself.

E.g.

<ui:repeat value="#{facesContext.messageList}" var="message">
    <h:panelGroup rendered="#{message.severity.ordinal eq 2}">
        ERROR! #{message.summary}
    </h:panelGroup>
</ui:repeat>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Is there a way to interpolate the value in the ValidationMessages.properties? e.g. "EMAIL_invalid={value} : Not valid" This is similar to "EMAIL_invalid={min}-{max} : Not valid" but I want to reference the validating object rather than the static `Annotation` parameters – Ioannis Deligiannis Nov 23 '13 at 11:17
  • See the edited answer which now contains a link to OmniFaces example. – BalusC Nov 23 '13 at 11:18
  • Thanks BalusC. Your first suggestion was good for me(`{1}: {0}`). What I was looking for was to do the interpolation on the Validation level. I created a new question as it is a bit different topic: http://stackoverflow.com/questions/20161837/custom-bean-validation-does-not-inject-cdi-beans-and-does-not-interpolate-mes – Ioannis Deligiannis Nov 23 '13 at 11:49