Different CSS style for JSF messages - jsf

In JSF, I have included the following line in my xhtml file:
<h:panelGroup id="messageHeader">
<h:messages for="messageHeader" layout="list" errorClass="error" infoClass="inform"/>
</h:panelGroup>
The text gets rendered as
<ul>
<li class="error"> Please enter a First Name </li>
<li class="error"> Please enter a Last Name </li>
</ul>
How do I get the CSS style to apply to the <ul> tag or some surrounding <span> tag?
Here is what I am trying to do. I want all error messages to appear in a single red box. I also want all info messages to appear in a single green box. The example above produces 2 red boxes; one for each <li> item.

Use the styleClass attribute of <h:messages>. It will be applied on the parent HTML element.
E.g.
<h:messages styleClass="messages" />
will end up as
<ul class="messages">
...
</ul>
Update: you seem to want to show info and error messages in separate lists. Use two <h:messages/> instead on which you hide the other severity.
<h:messages styleClass="errorMessages" infoStyle="hide" />
<h:messages styleClass="infoMessages" errorStyle="hide" />
with
.hide {
display: none;
}

This could be done by providing h:messages tag for each message type you need to show with appropriate condition. For exapmle for error message type:
<h:panelGroup id="errorMessageHeader" class="whatever you need" rendered="#{FacesContext.isValidationFailed()}">
<h:messages for="errorMessageHeader" layout="list" errorClass="error" />
</h:panelGroup>
Conditions for other message types you can find by combining methods from
javax.faces.context.FacesContext
Thanks,
A.K.

Related

p:repeat iteration over hashset variables behaves strangely

I have Set responseDocumentsVar in my view and it holds three values. For instance, PASPGP_CND,COVER_LTR,PERMIT_LTR. I used this variable in my xhtml and iterated using primesfaces component p:repeat as shown below. However the unexpected behavior is when I try to access "PASPGP_CND" letter, COVER_LTR is downloaded and when I try to access PERMIT_LTR ,PASPGP_CND is downloaded. Not sure why, what I access is not downloaded. Could anyone explain this behavior ?
<p:repeat value="#{sessionView.appCycleEntity.reviewResponseEntity.reviewResponseDocuments.toArray()}"
var="revResDocument"
width="100%" styleClass="hideDataTableColumnsHistory">
<div class="cardWrapper ui-g-12 ui-md-6 ui-lg-6">
<div class="cardDisplay fileCard">
<div class="cardTypeId">
<div class="icon"></div>
</div>
<div class="cardInfo">
<!-- card meta data -->
<dl class="cardMetaData clearfix">
<dt>
<p:commandLink id="filename${fileTypeCounter}_${fileCounter}"
value="#{sessionView.getLetterNameForSummaryScreen(revResDocument.letterTypeCode).length() gt 30 ? sessionView.getLetterNameForSummaryScreen(revResDocument.letterTypeCode).substring(0,30).concat('...') : sessionView.getLetterNameForSummaryScreen(revResDocument.letterTypeCode)}"
actionListener="#{recordDecisionController.retrieveLetter(revResDocument)}"
ajax="false" process="#this" onstart="PF('statusDialog').show()"
oncomplete="PF('statusDialog').hide();" immediate="true"/>
</dt>
</dl>
</div>
</div>
</div>
</p:repeat>
In above image when I try to access 1, 3 is download and when I try to access 3, 2 is downloaded i.e. desired file not downloaded. I debugged on the java side, the attachment id passed to backend is also not correct.
I am sorry if I was not clear in explaining the question.

How to prevent a4j:status from flickering when polled by a4j:poll

I am working on a project that uses a rich:tree element that, when expanded, should display a processing animation nearby using a4j:status. The problem that I am running into is that the tree is rerendered by multiple other elements. The one that I believe to be causing the most trouble is an a4j:push element that rerenders the tree every second. This rerendering is causing the status element to change states when it shouldn't, causing a flickering on screen.
My status element:
<a:outputPanel id="working_animation" style="display: block; height: 1px">
<!-- Working animation -->
<a:status stopText=" " id="header_progress">
<a:facet name="start">
<a:outputPanel id="header_working" rendered="#{rich:element('tree_state.value').value}" style="padding-left: 16.5em;">
<span id="l-1"></span>
<span id="l-2"></span>
<span id="l-3"></span>
<span id="l-4"></span>
<span id="l-5"></span>
<span id="l-6"></span>
</a:outputPanel>
</a:facet>
</a:status>
</a:outputPanel>
Part of the tree element:
<rich:tree
id="sectionTree"
switchType="ajax"
value="#{sectionAction.sectionNodes}"
var="node"
nodeFace="#{node.nodeType}"
rightClickSelection="true"
ajaxSubmitSelection="true"
iconCollapsed="/img/treenode/collapsed.png"
iconExpanded="/img/treenode/expanded.png"
componentState="#{sectionAction.treeState}">
<rich:changeExpandListener binding="#{sectionAction}" />
<rich:nodeSelectListener binding="#{sectionAction}" />
beyond that are node definitions
The push element:
<h:form>
<a:push
eventProducer="#{loggedUser.registerLoginListener}"
interval="1000"
reRender="sectionTree">
</a:push>
<a:push
eventProducer="#{loggedUser.registerStudentListener}"
interval="2000"
reRender="sectionTree">
</a:push>
</h:form>
There are two options how to disable status:
Wrap a:push tags by a:region.
Specify status="none" attribute for a:push.
Refer to the livedemo for more details: http://livedemo.exadel.com/richfaces-demo/richfaces/status.jsf?c=status&tab=usage

h:selectManyCheckbox loses values when navigating

My problem is that h:selectManyCheckbo loses values when navigating , here's my code below :
<h:selectManyCheckbox id="selectGroupsBox"
value="{userCreationWizardBean.selectedGroupsId}"
layout="pageDirection">
<f:selectItems var="Group"
value="#{userCreationWizardBean.terminalsGroups}"
itemLabel="#{terminalsGroup.groupName}"
itemValue="#{terminalsGroup.id}"
itemDisabled="#{Group.defaultGroup}">
</f:selectItems>
<p:ajax event="change" update=":form-top-wizard:numBase" process="selectGroupsBox"
listener="#{userCreationWizardBean.selectedGroupListner}"></p:ajax>
</h:selectManyCheckbox>
The CheckBox loses values if I go back and forth using my navigation pad :
<div id="top-wizard" class="#{class}">
<ul>
<li class="premier#{wizard_step=='0'?'_active':''}"><p:commandLink
action="#{userCreationWizardBean.getViewAt(0)}" ajax="true"
update=":msg-form:popup_faces-messages"
/></li>
<li class="deuxieme#{wizard_step=='1'?'_active':''}"><p:commandLink
action="#{userCreationWizardBean.getViewAt(1)}" ajax="true"
update=":msg-form:popup_faces-messages"
/></li>
<li class="troisieme#{wizard_step=='2'?'_active':''}"><p:commandLink
action="#{userCreationWizardBean.getViewAt(2)}" ajax="true"
update=":msg-form:popup_faces-messages"
/></li>
<li class="quatrieme#{wizard_step=='3'?'_active':''}"><p:commandLink
action="#{userCreationWizardBean.getViewAt(3)}" ajax="true"
update=":msg-form:popup_faces-messages"
/></li>
</ul>
</div>
The problem stragely vanishes when I delete the itemDisabled attribute.
it seems like i can use jquery css selector syntaxe to exclude the checkbox from being processed , but only in primefaces 3.3 , but i'm using 3.2.
I believe the issue you are experiencing has to do with the fact that a disabled component or item's value is not sent to the server.. I Have also been trying to find a workaround. check out this post it might be helpful.

Validation failed javascript callback in JSF

I have a template in which I can add a CSS error class to a div when the validation of a component has failed and it renders a pretty nice effect on the browser.
Now, I don't need to add a css class to a component (this won't help me), but rather I need to change the css of the html that surrounds it, this is pretty simple with jQuery, however I can't seem to find a javascript callback for failed validation, is this possible? I'm also using primefaces (in case they provide such capabilities).
Markup:
<div class="control-group ERROR_CLASS_GOES_HERE_IF_VALIDATION_FAILED">
<label class="control-label">Input value:</label>
<div class="controls">
<h:inputText class=" inputbox" type="text" required="true" /> <!--Component that can fail -->
</div>
</div>
if the input text is empty, I need the div that wraps the "control group" to have an extra class. I can turn it into a <h:panelGroup> so it is a JSF component but still I wouldn't know how to do it. Javascript seems easier as I can do a:
jQuery("#ID_OF_DIV").addClass("error_class")
Just let JSF/EL conditionally print the class based on FacesContext#isValidationFailed().
<div class="control-group #{facesContext.validationFailed ? 'error_class' : ''}">
You only need to ensure that this element is covered by ajax update/render.
Another way would be hooking on the oncomplete event of an arbitrary PrimeFaces ajax based component. There's an args object available in the scope which in turn has a validationFailed property. E.g. <p:commandButton oncomplete> or even <p:ajaxStatus oncomplete>.
<p:ajaxStatus ... oncomplete="if (args && args.validationFailed) $('#ID_OF_DIV').addClass('error_class')">
If you want to do everything on the client side.
<h:outputText class="samplecls" rendered="#{facesContext.validationFailed}"
value="Please enter all the required fields">
</h:outputText>
<div class="control-group ERROR_CLASS_GOES_HERE_IF_VALIDATION_FAILED">
<label class="control-label">Input value:</label>
<div class="controls">
<h:inputText class=" inputbox" type="text" required="true" /> <!--Component that can fail -->
</div>
</div>
Javascript/Jquery
This class will exist in DOM only validation fails by rendered="#{facesContext.validationFailed}"
$(window).load(function(){
if($('.samplecls').length>0){
$("#ID_OF_DIV").addClass("error_class");
}
});

How to use li and ul tag in JSF Framework?

How to use li and ul tag in JSF Framework?
You can use standard html mixed with JSF syntax. Just write
<ul>
<li> Apple </li>
<li> Banana </li>
</ul>
I guess what you want is displaying a list from a List
If you are using Facelets, do:
<ul>
<ui:repeat value="#{myBean.items}" var="item">
<li><h:outputText value="#{item.name}"/></li>
</ui:repeat>
</ul>
If you are not, you should. Anyway, you can do the same with the old JSTL and c:forEach
<html ... xmlns:c="http://java.sun.com/jstl/core">
...
<ul>
<c:forEach items="#{myBean.items}" var="item">
<li><h:outputText value="#{item.name}"/></li>
</c:forEach>
</ul>
You can also use Tomahawk's t:dataList to render a <ul><li> in a "jsfish" way without bringing in "plain vanilla" HTML. Useful if you're still on JSF 1.0/1.1 yet and don't want to hassle with f:verbatim.
<t:dataList layout="unorderedList" value="#{bean.list}" var="item">
<h:outputText value="#{item}" />
</t:dataList>
which generates
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
which look like this
item 1
item 2
item 3

Resources