My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

vineri, 5 februarie 2016

Validating PrimeFaces Rating components via OmniFaces ValidateOneOrMore validator

Let's suppose that we have a rating system based on PrimeFaces Rating component. Basically, we have three rating components that allows to the user to express his opinion about the main shots of a tennis player: serve, forehand and backhand. The user should rate at least one of these three shots. So, the code without this restriction will look like this:

<h:form>
 <p:messages/>

 Rating Rafael Nadal favorite shots:

 <p:panelGrid columns="2">
  <p:outputLabel for="serveId" value="Serve:"/>
  <p:rating id="serveId" value="#{playerBean.serve}" />

  <p:outputLabel for="forehandId" value="Forehand:"/>
  <p:rating id="forehandId" value="#{playerBean.forehand}"/>

  <p:outputLabel for="backhandId" value="Backhand:"/>
  <p:rating id="backhandId" value="#{playerBean.backhand}"/>

  <p:commandButton value="Submit" update="@form"/>
 </p:panelGrid>
</h:form>

Further, we must ensure that the user will rate at least one type of shots which involves some validators. A very quick approach consist in adding into equation the OmniFaces ValidateOneOrMore validator. This is just one of the suite of multi-field validators provided by OmniFaces and it is capable to validate if at least ONE of the given UIInput components has been filled out.

In order to use it, you need to add OmniFaces into your application (this task is very easy to accomplish and similar with PrimeFaces; you can simply copy the OmniFaces JAR into your application or use the Maven dependency). Further, you have to know that ValidateOneOrMore validator is exposed to page authors via <o:validateOneOrMore/> tag. This tag requires an attribute named components whose value is a string of components client IDs separated by space. The referenced components enter into validation. For example, we have used it as below:

<h:form>
 <p:messages/>

 Rating Rafael Nadal favorite shots:

 <p:panelGrid columns="2">
  <p:outputLabel for="serveId" value="Serve:"/>
  <p:rating id="serveId" value="#{playerBean.serve}" />

  <p:outputLabel for="forehandId" value="Forehand:"/>
  <p:rating id="forehandId" value="#{playerBean.forehand}"/>

  <p:outputLabel for="backhandId" value="Backhand:"/>
  <p:rating id="backhandId" value="#{playerBean.backhand}"/>

  <p:commandButton value="Submit" update="@form"/>

  <o:validateOneOrMore id="oneormore" components="serveId forehandId backhandId" />
 </p:panelGrid>
</h:form>

That's all! In the screenshots below, you can see how the validation works:

Invalid case:
Valid cases:

The validation error message can be altered via the optional attribute, message or via message bundle. More details about using the OmniFaces multi-field validators are available here. You also may want to consider this SlideShare presentation.

The complete application from this post is available here.

Read more such goodies in:

PrimeFaces & OmniFaces - Powers Combined

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors