p:messages working, p:message isn't.

UI Components for JSF
Post Reply
Alan
Posts: 102
Joined: 25 Oct 2010, 04:45

12 Sep 2011, 17:57

Using 3.0.3.M3 here. I think I hit some obvious error but I can't see what it is. I have a little form like this:

Code: Select all

<h2>Create New Category</h2>
                    <h:form id="mqCreateForm">
                        <h:panelGrid cellspacing="4" columns="3">

                            <h:outputLabel value="Category Name:" />
                            <p:inputText id="mqCat" size="30" 
                                         value="#{maintCategory.newCategory.name}"
                                         />
                            <p:message for="mqCat"  />
                            
                            <h:outputLabel value="Count:" />
                            <h:outputText  value="#{maintCategory.count}" />

                        </h:panelGrid>
                        <p:commandButton value="Create New Category"
                                         actionListener="#{maintCategory.doNewCategory}" 
                                         update=":mqCreateForm :mqForm:mqDataTable" />
                        <br/>
                        <p:messages />
                    </h:form>
When I hit the button, the following code executes in the bean, submitted via AJAX:

Code: Select all

        FacesContext fc = FacesContext.getCurrentInstance();
        FacesMessage fm = null;
        
        String s = newCategory.getName();
        if (s == null || s.isEmpty()) {
            fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Not Created", 
                    "Category Name May Not Be Empty");
            fc.addMessage("mqCat", fm);
        }
        
        // if an error occured, just return
        
        if (fm != null) return null;
The update attribute on the commandButton seems to be working just fine. Also, the summary message appears where the p:messages tag is, but nothing ever appears where the p:message tag is.

The showcase example doesn't show this pattern -- where an fc.addMessage(clientID .. case is used. Is this supposed to work or am I doing something wrong? This seems like a textbook example.
Netbeans 7.1, GlassFish 3.1.1, MacOS X 10.6.8, Safari, Firefox

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

12 Sep 2011, 20:32

It's a textbook example with a textbook error (I suspect). Form is a naming container, so the its id is prepended to the id's of all its children. So you need to add it to the 'for' in the p:message and in the java code

Alan
Posts: 102
Joined: 25 Oct 2010, 04:45

13 Sep 2011, 01:09

Ok, then. That did turn out to be the right path.

I changed the facelet to:

Code: Select all

 <p:message for="mqCreateForm:mqCat"  />
and the backing bean now has:

Code: Select all

fc.addMessage("mqCreateForm:mqCat", fm);
and that works.

Did PF change its behavior on this in some recent version? I am using 3.0.M3 now, but I could swear that on an earlier version I had facelet/backing bean pairs that did not need the clientID form (with the form ID prepended). If it changed, is this the way that it is going to stay?

One other thought: are the rules different in the case of a partial submission versus a full page submission?
Netbeans 7.1, GlassFish 3.1.1, MacOS X 10.6.8, Safari, Firefox

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

13 Sep 2011, 10:47

I am using 3.0.M3 now, but I could swear that on an earlier version I had facelet/backing bean pairs that did not need the clientID form (with the form ID prepended)
Has nothing to do with PF, but that you had a version of your app that did not need this could very well be the case, since the textbooks also show that h:form has something called 'prependId' which is a boolean attribute. Guess what is does when set to false :-) (Searching a little on google might have given this answer and on the initial one ;-))

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

13 Sep 2011, 10:49

This is how JSF works and we followed the same when implementing the p:message component and haven't changed/extended anything since then.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 52 guests