How to set inputText to required without affecting output label in Primefaces?

I would recommend using the plain JSF <h:ouputLabel… />

<h:outputLabel value="Target Species" for="idInputText" />  
<p:inputText id="idInputText" required="true" value="#{controller.string}"/>

This removes the asterisk but keeps the label correctly associated with the input element. This is important for accessibility.


Not sure if this also works for 4, but it does for PrimeFaces 5.3: simply add indicateRequired="false". So:

<p:outputLabel value="Target Species"
               for="idInputText"
               indicateRequired="false"/>  
<p:inputText id="idInputText"
             required="true"
             value="#{controller.string}"/>

Another option is to use css to hide the asterisk:

.ui-outputlabel-rfi { display: none; }

Then the label will still be associated with the input, and you can still use a Label Provider if you like:

http://cagataycivici.wordpress.com/2011/02/11/label-provider-for-jsf-input-components/

Tags:

Jsf

Primefaces