What's the best way to show failed validation messages?

UI Components for JSF
Post Reply
User avatar
Crystal
Posts: 18
Joined: 20 Jul 2010, 22:38
Location: CT. USA

24 Jul 2010, 00:54

Hello,

I have been trying to find the best component to display messages next to the text boxes if it failed validation in the bean.
It seems like the message component will do that but I cannot figure out how to implement it.
createSobe.xhtml

Code: Select all

<p:messages id="msgs"/>
            <h:panelGrid columns="2">
                <h:outputText value="Manufacturer" />
                <h:inputText value="#{create.manufacturer}"/>
I cannot find sample code for how this is implemented in the bean.
my xhtml page no longer loads. It says: Cannot find component "manufacturer" in view

So I think I am doing this completely wrong.

If there is documentation on how to implement this please point me to it. The code on the message component showcase page does not show any bean code. Or maybe I am totally confused and the bean cannot send error messages to this component. If that's the case what do I use?
Thanks!
Using primefaces-2.1.RC1.jar, Mojarra JSF 2.0.3, GlassFish 3

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

24 Jul 2010, 02:16

You need to use for attribute of p:message, an example;

Code: Select all

<h:panelGrid id="grid" columns="3">
		<h:outputText value="Text: *" />
		<h:inputText id="text" required="true" />
		<p:message for="text" />
	</h:panelGrid>

User avatar
Crystal
Posts: 18
Joined: 20 Jul 2010, 22:38
Location: CT. USA

24 Jul 2010, 03:30

Thanks.
It looks like your example will show an error message if 'text' is blank.

I have to validate data in a backing bean and put error messages on the fields that didn't pass. Can this be done using <p:message ?
Sorry but I'm still learning JSf & PrimeFaces.

Thanks!
Using primefaces-2.1.RC1.jar, Mojarra JSF 2.0.3, GlassFish 3

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

24 Jul 2010, 13:07

Sure, you need to add FacesMessage manually using the client id of the component.

Code: Select all

<h:form id="form">
  <h:panelGrid id="grid" columns="3">
      <h:outputText value="Text: *" />
      <h:inputText id="text" required="true" />
      <p:message for="text" />
   </h:panelGrid>
</h:form>

Code: Select all

FacesContext.getCurrentInstance().addMessage("form:text", new FacesMessage("Text is not valid"));
or if you set prependId="false" on form you can use;

Code: Select all

FacesContext.getCurrentInstance().addMessage("text", new FacesMessage("Text is not valid"));

User avatar
Crystal
Posts: 18
Joined: 20 Jul 2010, 22:38
Location: CT. USA

25 Jul 2010, 05:32

Thanks for the example!

It works great. :)
Using primefaces-2.1.RC1.jar, Mojarra JSF 2.0.3, GlassFish 3

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 41 guests