Get rid of extra message when using h:message - jsf

In my JSF 2.0 application, whenever validation fails, I display a message to the user with <h:message> tag. I always get one extra message at the top of the page along with the expected one. How can I get rid of this?

Apparently you're using a <h:messages> in the top of the page. If you set it to display global messages only (those with a null client ID, as in context.addMessage(null, message)), then the client-specific message won't be duplicated over there.
<h:messages globalOnly="true" />

Related

JSF Required Message being displayed twice

Here is the senorio and codes. JSF Required Message being displayed twice. how can i solve?
<h:outputText value="#{msg['text.surdurulebilir']} :"/>
<p:rating value="#{appraisalBean.newAppraisal.ratingSustainability}" stars="10"
readonly="#{sessionBean.loggedUser.fullName == 'Admin' or sessionBean.loggedUser.fullName == 'Moderator'}"
required="true" requiredMessage="Sürdürülebilirlik Değerlendirilme Alanı Boş Bırakılamaz." id="star5"/>
please check your p:messages tag and both showDetails and showSummary attribute are set to true. That's why two messages are displaying one for summery and another for detail. One more doubt I have you may be adding messages from java file as well

Primefaces - at least one field required with one message for all fields

Is it possible to perform primefaces validation to validate that one field in the form has to be filled in? When none are filled in, all fields have to be marked red (happens automatically) and only one message may be displayed in the messages tag. Right now a message is displayed for each required field and I don't want that.
I've tried:
<p:messages id="messages" redisplay="false"/>
but this doesn't filter them.
An example of one of the many inputFields:
<p:inputText id="from" value="#{containerBean.selectedContainer.from}"
maxlength="512"
requiredMessage="#{msgs['one.field.required']}"
required="#{empty param['containerForm:description']
and empty param['containerForm:to']
and empty param['containerForm:year']
and empty param['containerForm:yearTo']
and empty param['containerForm:sequence']}"
disabled="#{documentBean.addNewOrUpdateDocumentSelected or !containerBean.canEditSelectedContainer()}"/>
I know it's possible with omnifaces, but I may not use it :)..
PF 5.3
I also know that I could perform the validation in my BackingBean, but that's not a good practice..

Update JSF messages after initial rendering via p:menuitem

Does anyone know if it's possible to update a JSF message or messages element AFTER its initial rendering via PrimeFaces' p:menuitem?
For example, I have a PrimeFaces p:menuitem I am using to open a p:dialog, and that p:dialog has a p:messages element whose message I would like to update and show the moment that the p:dialog is opened - I can't know whether there is a message I want to show or what that message should be until after the p:menuitem's action method is complete.
Unfortunately, setting the update attribute of the p:menuitem to both the id of the p:dialog and the id of the p:messages element itself does not cause the p:messages element to show its message, even though I think it should since the action method of the p:menuitem looks like it is properly adding a message to the p:messages element.
The menuitem opening the dialog:
<p:menuitem value="Show Dialog with Message" action="#{myView.prepareDialogWithMessage()}" oncomplete="PF('dialogWidget').show()" update="dialogWithMessage message" />
The dialog with the message:
<p:dialog id="dialogWithMessage" widgetVar="dialogWidget" resizable="false" dynamic="true" closable="false" showEffect="fade" hideEffect="fade">
<p:messages id="message" for="message" showDetail="true" escape="false" autoUpdate="true"/>
<div class="button-panel">
<p:commandButton value="Yes" styleClass="ui-confirmdialog-yes" action="#{myView.submitAction()}" oncomplete="PF('dialogWidget').hide();"/>
<p:commandButton value="No" styleClass="ui-confirmdialog-no" onclick="PF('dialogWidget').hide();"/>
</div>
</p:dialog>
The Java code which adds the message:
public void prepareDialogWithMessage() {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Summary of message we want to show when the dialog opens", "Details of the message we want to show when the dialog opens");
FacesContext.getCurrentInstance().addMessage("message", message);
}
Short answer: It behaves exactly as expected and implemented.
Longer answer:
Having a closer look after your edited question, I noticed the dynamic="true" on the dialog. If you read the PrimeFaces docs it states (as you already found out):
dynamic: Enables lazy loading of the content with ajax.
So when you show the dialog via javascript, its content is updated... Since this content also includes the p:messages, that is also updated. In your case updated again with the messages that are generated in the ajax call of the update of the dialog... Most likely none... So the messages you added to it in the prepareDialogWithMessage() call of the menuItem are lost.
Since you already update the dialog in the call to the menuItem, the dynamic='true' is totally superfluous and it throws a spanner in the works.
<off-topic>A suggestion is to always start creating an [mcve]. Removing more and more until the behaviour of certain parts is as expected. Removing the dynamic='true' in one of the steps would have resulted in this and you'd have known a lot more. At the same time, also try debugging more. Investigating by looking at the requests and responses in the browser developer tool. You would have seen the messages getting added to the dialog and then the dialog(contents) being totally overridden. All these things are easy too (sorry, easy instead of 'not to difficult), can/will tell you a lot and helps getting to the cause of things quicker. Either directly, or by being able to ask way more specific question that is often easier to answer</off-topic>

How do I use multiple h:messages or p:messages on one page?

I am using PrimeFaces p:messages but I think this question applies equally to h:messages
I have a composite component to handle login/logout at the top of every page. (It is in the Facelets template.) To handle login errors, it has a p:messages component in it.
In the content area, I typically have a form that also has a p:messages in it. However if the form processing ever produces a message, I end up getting a message rendered from the login component as well as the place where I intended.
This is hardly a fatal problem, but I would like to clean this up. Any suggestions?
Primefaces p:messages has an attribute redisplay. Set
redisplay="false"
to instruct the message tag to only display messages that are not yet displayed on the page.

How to show a personalized error/warning/info message in a popup window?

Hi I want to show a popup message about errors or warnings in my app, for example if a user try to enter a registry that is already in the data base. I'm working with JSF and my app doesn't add a registry that is already in the data base but I need to show a message that informs the user about that.
Thanks.
This answer is specifically for icefaces (since I read in comments that the #Mel is working with icefaces.
To open a popup in icefaces:
<ace:dialog widgetWar="mydialog">
<h:outputText value="This is a dialog" />
</ace:dialog>
Then call this popup in your xhtml code like this:
E.g. I am using a command link that when clicked opens the popup.
<h:commandLink onlick="mydialog.show()" action="#{myBean.doSomething()}" />
To show messages in dialog:
<h:form id="myform">
<ace:dialog id="mydialog" widgetWar="mydialog">
<h:panelGrid columns="1" width="100%">
<ace:messages id="allMsgsAdd" initEffect="highlight" for="myform:mydialog" />
</h:panelGrid>
<h:outputText value="This is a dialog" />
</ace:dialog>
</h:form>
Then you can use this code to push messages to the ace:message you just created.
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(SEVERITY_INFO, "My Message", null));
Please note that SEVERITY_INFO is a FacesMessage defined constant. You will need to import it. Depending on whether you need to show warning or error to user, you can import these
import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
import static javax.faces.application.FacesMessage.SEVERITY_INFO;
Also note the first parameter of addMessage() method. You will notice that I am passing null as the first paramter. See here, and you will find that the first paramter is the client id, which basically means that which client do you want to send this message to. If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance. So all of the message dialogs in your page will receive this error/warning. If you wish to ONLY show the message on the dialog, you need to pass your dialog client id here like this:
FacesContext.getCurrentInstance().addMessage("myform:mydialog", new FacesMessage(SEVERITY_INFO, "My Message", null));
You can try to use some existing open source JSF component library .Many of them already provide the pop-up UI . For example ,Richfaces provides it by . You can refer to this official link to see how to use : http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/rich_modalPanel.html
Check my asnwer in another question, maybe it will be useful for you
How do I get a richfaces modal window to display without an onclick event?
This will show your exception messages (or another message added by you in middle of the code) in a <rich:modalPanel>.

Resources