condition in tag xhtml with JSF2 - jsf-2

I have this part of code using primeface component :
<h:outputText value="Division : " />
<p:inplace id="basic2">
<p:inputText value="#{utilisateursController.u1.division}" />
</p:inplace>
I want to testing if #{utilisateursController.u1.division} is equals "" (or null) it displays "Please enter a valid division" otherwise, it displays the value of #{utilisateursController.u1.division}
I have already see that somewhere but I don't know how to use
can you remember me ?
thanks

Something like this ?
<p:inputText value="#{(not empty utilisateursController.u1.division)?utilisateursController.u1.division:'Please enter a valid division'}" />

Related

Display JSF element only if specific h:message is being displayed

I have a h:form with several inputs and each of them got its own h:message and I'm looking for a way to show (using render or assigning some styleClass) some other element only when specific h:message is being shown (and hide when that h:message is not being displayed).
Here a code snippet
<li>
<h:panelGroup id="current_password_wrapper">
<p:password value="#{myBean.myCurrPass}" id="current_password" required="true"
styleClass="required_field" />
</h:panelGroup>
<h:message for="current_password"/>
</li>
<li>
<h:panelGroup id="new_password_wrapper">
<p:password value="#{myBean.myNewPass}" id="new_password" required="true"/>
</h:panelGroup>
<h:message for="new_password"/>
<h:commandLink value="my value"/>
</li>
I want to make the h:commandLink visible only when the <h:message for="new_password"/> is being displayed
So far I couldn't find anything...
If your environment supports EL 2.2, then you could check if FacesContext#getMessageList() isn't empty for the particular client ID.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not empty facesContext.getMessageList(new_password.clientId)}" />
If the message is being shown as result of a validation error, then you could also just check the UIInput#isValid() state of the component associated with the message.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not new_password.valid}" />
Note that manually adding a faces message to the context won't mark the input component invalid. Therefor either a true Validator should be used which throws a ValidatorException, or an explicit input.setValid(false) call has to be done programmatically.
I think with the answer to this question you requirement can be archived:
How to number JSF error messages and attach number to invalid component
I think you can do something like this:
<h:outputText value="output text" rendered="#{bean.messageIndexes['form:input1'] > 0}" />

jsf validation for datatable

I want to validate the below datatable only if user doesn't enter any values in the given p:inputText field
<p:dataTable id="depositDataTable" value="#{pc_intimationDeposit.pendingRep.depositeBeans}" var="deposit">
<p:column style="text-align:right">
<h:outputText value="#{deposit.depParticulars}" />
</p:column>
<p:column id="value">
<h:inputText id="depositDetails" class="right_input" value="#{deposit.amnt}" tabindex="2" converterMessage="Please Enter Numbner's Only" validatorMessage="please">
<f:convertNumber pattern="##,####,##0.00" for="depositDetails" type="currency" />
</h:inputText>
<p:message id="errMsgDepositDetails" for="depositDetails" display="text"></p:message>
</p:column>
</p:dataTable>
pc_intimationDeposit.pendingRep.depositeBeans----> is a list(size 3) of depositBeans
i have used f:convertNumber for the entered values are correct to our requirements
but if user doesn't enter A SINGLE VALUE then i need validate for null check
i.e., user has to enter any one field
and i need to display the error message on top of the datatable used
please help me
Thank in you advance
If you want to have every depositDetail inputs populated, mark
them with required=true.
If you only want to have one of them filled, on your action method,
iterate over your model to ensure there's at least one not empty (you shouldn't have null values here):
boolean filled = false;
for (DepositeBean dep : depositeBeans){
if (!dep.getAmnt.equals("")){
filled = true;
}
}
if (!filled){
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to fill at least one deposit detail field", null));
}
For messages to be displayed, add an h:message tag and point it to your table:
<h:message for="depositDataTable" style="color:red" />

Primefaces inputtext focus at the end of the text

Here a snippet of my JSF:
<p:tabView id="tabView" var="tab" value="#{data.tabs}" widgetVar="tabViewWidget"
activeIndex="#{data.newActiveTabIndex}">
<p:ajax event="tabClose" listener="#{logic.closeTab}" />
<p:tab title="#{tab.content.title != null ? tab.content.title : '&lt;new&gt;'}"
closable="#{tab.isClosable and data.tabs.size() > 2}">
<h:outputText value="#{tab.id} Test" />
<p:focus rendered="#{data.componentIdToFocus != null}" for="#{data.componentIdToFocus}" />
<p:inputText id="test" value="#{tab.content.title}">
<p:ajax event="keyup" listener="#{logic.setFocusingComponent}" update=":form:tabView" />
</p:inputText>
</p:tab>
</p:tabView>
The intent is that there is a textfield within each tab that updates the title of the tab while writing.
The given snippet works except that p:focus places the cursor at the beginning of the text that is already written. I want the cursor to be placed at the end of the text already written. So that a user can type and while he/she is typing the title adapts automatically.
I hope my case is clear.
Does anybody have a solution for that?
Best regards,
Florian
You can use the onfocus="this.value=this.value" trick to "deselect" the value so that the cursor automagically ends up in the end of the value. This should work as good for the <p:inputText> as it basically generates just a <input type="text"> element.
<p:inputText ... onfocus="this.value=this.value" />
See also:
Use JavaScript to place cursor at end of text in text input element
Unrelated to the concrete problem, I wouldn't use ajax and such on keyup event and an update of the whole tabview. This seems to be quite expensive. I'd rather do a simple HTML DOM traversion and manipulation here by JS/jQuery.
E.g.
<p:inputText ... onkeyup="updateTitle(this)" />
with
function updateTitle(inputInTab) {
var panelId = $(inputInTab).closest(".ui-tabs-panel").attr("id");
$("a[href='#" + panelId + "']").text(inputInTab.value);
}

Autocomplete not working in some condtion

I am using primefaces autocomplete component with itemtip option,
after getting suggestions I am selectiing one value as a example lastname,
but I want to display in textbox both lastname and firstname of player, so tried in this way.
itemLabel="#{p.lastName} #{p.firstName} " having some space with these two .
<p:autoComplete id="watermark" value="#{backingBean.object}"
size="40" completeMethod="#{backingBean.completeholder}"
var="p"
itemLabel="#{p.lastName} #{p.firstName} " itemValue="#{p}" converter="player">
<f:facet name="itemtip">
<h:panelGrid columns="2" styleClass="cellsp-panel">
<f:facet name="header">
<p:graphicImage value="#{p.imagePath}" width="60"
height="60" />
</f:facet>
<h:outputText value="LastName " style="font-weight:bold" />
<h:outputText id="ln" value="#{p.lastName}" />
<h:outputText value="FirstName " style="font-weight:bold" />
<h:outputText id="fn" value="#{p.firstName}" />
</h:panelGrid>
</f:facet>
<p:ajax event="itemSelect" update="e,c">
</p:ajax>
</p:autoComplete>
so after selecting a suggestion, it displays lastname and firstname of player perfectly.
But one problem is created as - it creates a space in autocomplete textbox, so i need to use
backspace to remove this space so that I can search a for getting suggestions.
How can I overcome from this problem?
Also I am using watermark on autocomplete , it works only when I use single itemLabel like
itemLabel="#{p.lastName} otherwise it unable to display watermark in autocomplete.
3.Also when I use for no-case sensitive input so that user can search without case sensitive,
once I get any suggestions and still if try to type some keyworks for further filterations,
suggestions disappears,it works if I go for case sensitive by default way.
User can type players lastname or firstname , and still he needs to gets suggestion ,what modifications should I need to done in following method ?
I have stored lastname with First letter as a Capital, so I am using toUpperCase() in this way
public List completePlayer(String query) {
List suggestions = new ArrayList();
for(Player p : players) {
if(p.getlastName().startsWith(query.toUpperCase()))
suggestions.add(p);
}
return suggestions;
}
How can I overcome from this problem?
Your player compare is resolving to this:
Smith.startsWith("S") -> true
Smith.startsWith("SM") -> false
What you want is:
if(p.getlastName().toUpperCase().startsWith(query.toUpperCase()))
The space in your autocomplete is coming from this line:
itemLabel="#{p.lastName} #{p.firstName} " itemValue="#{p}" converter="player">
You have a space after #{p.firstName} in itemLabel before your closing quote.
As for the watermark, I don't know. I have never tried it with an autocomplete. Can you include your p:watermark tag so we can see what it is referencing?

Error when submit a <p:pickList> with <p:inputText> column

I need the pickList with a editable column, I'm not sure is it possible.
pickList:
<p:pickList value="#{model.formatList}"
var="format"
itemValue="#{format.formatName}"
converter="formatConverter">
<p:column>
<h:outputText value="#{format.formatMess}" />
</p:column>
<p:column>
<p:inputText value="#{format.width}" />
</p:column>
</p:pickList>
In my Backing Bean, all the field in formatList have getter&setter.
When I submit the form,some error message show in console:
[javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-0.0.0.0-0.0.0.0-8080- 1)/page/lookup/lookupFormatEdit.xhtml #91,45 value="#{format.width}" : Target Unreachable, identifier 'format' resolved to null: javax.el.PropertyNotFoundException ...
But if I replace <p:inputText value="#{format.width}" /> with <h:outputText value="#{format.width}" />, it works.
Anyone could give me some suggestion,thx.
<p:pickList> is simply a list of things for you to pick. I don't think you can have a <p:inputText> inside a <p:pickList>. I think you should use <p:dataTable> with multiple row selection instead. Then you can put <p:inputText> in 1 of the columns.
Use
p:inplace
tag of primefaces.
It will work definitely.

Resources