I’m currently trying simple validation using required=”true”
<h:form>
<h:messages globalOnly="true"/>
<h:panelGrid>
<h:panelGrid columns="3">
<f:facet name="header">
Login Application Sample
</f:facet>
<h:outputLabel for="UserId" value="User Id" />
<h:inputText id="UserId" value="#{userBean.userId}" required="true" />
<h:message for="UserId"/>
<h:outputLabel for="Password" value="Password" />
<h:inputSecret id="Password" value="#{userBean.password}" required="true" />
<h:message for="Password" />
<f:facet name="footer">
<h:commandButton value="Login" action="#{userBean.login}"/>
<h:commandButton type="reset" value="Reset"/>
</f:facet>
</h:panelGrid>
</h:panelGrid>
</h:form>
Leaving the fields blank, and then clickin on the login button, these error messages will display on the right side of each field :
j_idt7:UserId: Validation Error: Value is required.
j_idt7:Password: Validation Error: Value is required.
This is what I expected, but I don’t want to display the form id prefix of ‘j_idt7:’. I read book examples, they don’t output the form id prefix. What I want is :
UserId: Validation Error: Value is required.
Password: Validation Error: Value is required.
What should I do to skip displaying the form id prefix in the component specific messages ?
I’m currently testing JSF 2 in glassfish v3.
The message label defaults to component’s client ID, exactly the one as you can see in generated HTML output via rightclick, View Source. That
j_id7is in this particular case the client ID of the parent<form>element. If you give the JSF component a fixed ID like<h:form id="login">then the labels will becomelogin:UserIdandlogin:Passwordrespectively.You can however use the input component’s
labelattribute to override it altogether so that the message label will be shown exactly as you intented.If the input component’s
labelattribute is present, then it will be used instead of the client ID. UsingprependId="false"as suggested by other answers has disadvantages. Don’t do that.A completely different alternative is to use
requiredMessage(orconverterMessageorvalidatorMessage) attribute for this, but this doesn’t allow parameterizing messages and thus you’d have to hardcode the labels and such.See also:
Noted should be that it’s indeed awkward to have labels duplicated like this:
If you happen to use JSF utility library OmniFaces, then you can use
<o:outputLabel>to let JSF transparently set thelabelattribute of the associated component: