How to make one field from two required at least with JSF/Primefaces - jsf

I'm using primefaces with jsf and i want to make one of two fields required at least. that means that the error message will be displayed if this two fields are empties togheter:
this is a sample of my code:
<h:outputLabel for="srcNumber" value="Originator MSISDN (EXAMPLE 32495959595)" />
<p:inputText id="srcNumber" value="#{cdrMmscRecBean.srcNumber}" label="srcNumber" />
<h:outputLabel for="destNumber" value="Destination MSISDN (EXAMPLE 32495959595)" />
<p:inputText id="destNumber" value="#{cdrMmscRecBean.destNumber} label="destNumber" />
thanks :)

You can implement it this way:
<p:inputText id="srcNumber" value="#{cdrMmscRecBean.srcNumber}" label="srcNumber"
required="#{empty cdrMmscRecBean.destNumber}" requiredMessage="SRC Number Required">
<p:ajax event="change" update="destNumber" />
</p:inputText>
<p:inputText id="destNumber" value="#{cdrMmscRecBean.destNumber}" label="destNumber"
required="#{empty cdrMmscRecBean.srcNumber}" requiredMessage="DEST Number Required">
<p:ajax event="change" update="srcNumber" />
</p:inputText>
For more reference on how to parametrize your validation message:
Parametrized Messages in JSF with Facelets Taglib Functions

If you want to show the validation error use <p:message for="srcNumber" />
and same for test number, get rid of your outputLabels, this will show the validations warnings.
You neeed to add the required="true" flag to your inputTexts as well.
This is primefaces
EDIT
Purpose of the h:outputLabel and its "for" attribute this here shows the outputLabel for using non primefaces to show validatoin messages if this is all your problem was then u just need to add the required="true" validation flag indicators on your input texts

Related

Invoking a text box and action method for several input text fields?

I'm new to jsf and not sure how to frame the question in a right way. I'm doing a name lookup functionality and my input form is as follows:
Accept input from a text field.(Backed by managed bean and
getters and setters)
Invoke the action using command button.
Display it back on the UI.
I'm not able to paste the entire code because of confidentiality.
<h:form>
<p:outputLabel value="ABC" />
<p:inputText id="a" value="#{mybean.variable}" size="10" />
<p:commandButton value="Check" actionListener="#{mybean.getName()}" update="name">
</p:commandButton>
<p:inputText id="name" value="#{mybean.Value}" />
</h:form>
The above mentioned works fine,However I have 5 input text fields and their corresponding action methods like as follows:
<p:outputLabel value="DEF" />
<p:inputText id="b" value="#{mybean.variable1}" size="10" />
<p:commandButton value="Check" actionListener="#{mybean.getName()}" update="name" >
</p:commandButton>
<p:inputText id="name" value="#{mybean.Value}" />
</h:form>
and so on.
When I put all the text boxes and their corresponding actions,the input is not passed to the backend and the console does not display anything.
Any help appreciated.
Thanks.

How to get rid of empty tooltips while displaying error messages on tooltips in PrimeFaces?

I display error messages somewhere on <p:tooltip> as follows.
<p:inputText id="text" value="#{bean.text}" required="true"/>
<p:tooltip for="text">
<p:message for="text"/>
</p:tooltip>
Although it displays an error message the given tooltip, an empty/unnecessary tooltip is shown, when there is no error as can be seen in the following picture - beside the bottom right corner of the text box.
How to get rid of such empty tooltips? (I tried someway but it did not work)
It can be done by checking for an error message in the list java.util.List<FacesMessage> that can be obtained by using facesContext.messageList.
The rendered attribute of <p:tooltip> can be set based on the error message/s found in the list for the associated component/s something along the line.
rendered="#{not empty facesContext.getMessageList('clientId')}"
A working code snippet :
<h:form id="form">
<p:panel id="panel">
<p:inputText id="text" value="#{bean.text}" required="true"/>
<p:tooltip for="text" rendered="#{not empty facesContext.getMessageList('form:text')}">
<p:message for="text"/>
</p:tooltip>
<p:commandButton value="Submit" update="panel"/>
</p:panel>
</h:form>
Or by using component binding. Such as,
<p:inputText id="text" binding="#{inputComponent}" value="#{bean.text}"/>
<p:tooltip for="text" rendered="#{not empty facesContext.getMessageList(inputComponent.clientId)}">
<p:message for="text"/>
</p:tooltip>
Or even
<p:inputText id="text" binding="#{inputComponent}" value="#{bean.text}"/>
<p:tooltip for="text" rendered="#{not inputComponent.valid}">
<p:message for="text"/>
</p:tooltip>
The last two cases are useful especially when the (input) component is enclosed within an iterating component like a <p/h:dataTable>, <p:dataGrid>, <p:dataList> (or even <ui:repeat>) where the uniqueness of enclosing components is determined based on the iterating row index of an iterating component such as, form:dataTable:0:text, form:dataTable:1:text, form:dataTable:2:text... and so on
p:tooltip should have a "rendered" attribute, set it to false
from documentation:
rendered : default=TRUE - value to specify the rendering of the
component, when set to false component will not be rendered.
Source: http://courses.coreservlets.com/Course-Materials/pdf/jsf/primefaces/users-guide/p-tooltip.pdf

rich:autocomplete rerendering labels

I have 2 fields in a jsf form. Field 1 uses rich:autocomplete, and field 2 uses inputText. I type invalid data in field 2 so that the validation message appears. Next, I start typing in field 1. As soon as I do this, the validation message in field 2 disappears. How can I prevent this?
<h:outputLabel value="Field 1:" escape="false"/>
<h:panelGroup>
<rich:autocomplete id="id1" mode="ajax" showButton="false"
autocompleteMethod="#{bean.method}"
autocompleteList="#{bean.aList}"
value="#{bean.field}"
required="true"
requiredMessage="Field 1 required">
<a4j:ajax event="blur" execute="#this" bypassUpdates="#{true}" render="id1,id1Msg" />
</rich:autocomplete>
<rich:message for="id1" id="id1Msg" />
</h:panelGroup>
<h:outputLabel for="id2" value="Field 2:"/>
<h:panelGroup>
<h:inputText id="id2" value="#{bean.field2}" style="width: 175px;" required="true" requiredMessage="Field 2 required" validatorMessage="Field 2 Please enter digits only" converterMessage="Field 2 - Please enter digits only">
<f:ajax event="change" execute="#this" bypassUpdates="#{true}" render="id2Msg"/>
</h:inputText>
<rich:message for="id2" id="id2Msg"/>
</h:panelGroup>
Set execute="#form" on id2. This will cause id2 to always trigger the submission of the entire form, even when it loses focus, causing the validation message to appear
execute="#this" on id2 as it stands means that only the parent component will be triggered
I got the desired behaviour by using execute="#form" on id1.

How to set inputText to required without affecting output label in Primefaces?

When I set an inputText to required, the the outputLabel I have associated with the inputText gets an asterisk added to it automatically. How do I prevent the asterisk from appearing?
<p:outputLabel value="Target Species" for="idInputText" />
<p:inputText id="idInputText" required="true" value="#{controller.string}"/>
I am using PrimeFaces 4.0
I would recommend using the plain JSF <h:ouputLabel… />
<h:outputLabel value="Target Species" for="idInputText" />
<p:inputText id="idInputText" required="true" value="#{controller.string}"/>
This removes the asterisk but keeps the label correctly associated with the input element. This is important for accessibility.
Not sure if this also works for 4, but it does for PrimeFaces 5.3: simply add indicateRequired="false". So:
<p:outputLabel value="Target Species"
for="idInputText"
indicateRequired="false"/>
<p:inputText id="idInputText"
required="true"
value="#{controller.string}"/>
Another option is to use css to hide the asterisk:
.ui-outputlabel-rfi { display: none; }
Then the label will still be associated with the input, and you can still use a Label Provider if you like:
http://cagataycivici.wordpress.com/2011/02/11/label-provider-for-jsf-input-components/
indicateRequired="true"
For example:
<p:inputText value="#{bean.firstName}" indicateRequired="true" required="true" requiredMessage="Name is required"/>
<p:outputLabel value="Target Species" for="idInputText" />
<p:inputText id="idInputText" required="true" value="#{controller.string}"/>
In your code ,you're specifically setting that label for inputText and this will have asterisk.
Remove "for" from outputLabel. It should look like :
<p:outputLabel value="Target Species" />
Now, you won't have asterisk .

Dynamically changing the visibility of the JSF components

My requirement is like this: I am having a text input and whenever a value change event occurs, a select many list box has to be populated. If there is no matching records found, then a text input has to appear instead of a select many list box.
<h:column>
<h:selectManyListbox size="3" value="#{hostInfoBean.gateKeeperendPointReference}" rendered="#{hostInfoBean.selectManyRendered}" id="gateKeeperendPointReference">
<f:selectItems value="#{hostInfoBean.gateKeeperendPointReferenceItems}" />
</h:selectManyListbox>
<h:inputText id="gateKeeperendPointReferenceText" size="30" rendered="#{!hostInfoBean.selectManyRendered}">
</h:inputText>
</h:column>
Also I am using a4j for the value change listener,
<a4j:support event="onchange" reRender="hostInfo:gateKeeperendPointReference" focus="GFacPath"
ajaxSingle="true" />
'selectManyRendered' is a boolean value which I am determining in the JAVA bean. The program works only for the default value of the boolean variable. If the boolean value is changed during runtime, then the toggle between the visibility of selectManyListbox and inputText is not working. Please help to fix this. Am i missing something?
regards,
Suresh
If the "rendered" attribute resolves to false, then the component isn't in your tree and can't be found as a "rerender" target. When you have components that are rendered conditionally you want to wrap them in a component that is always available as a target, like so:
<h:inputText value="#{myBean.text}" >
<a4j:support event="onkeyup" reRender="listZone" ajaxSingle="true" />
</h:inputText>
<h:panelGroup id="listZone">
<h:selectManyListbox value="#{myBean.list}" rendered="#{myBean.renderList}" >
<f:selectItems value="#{myBean.listItems}" />
</h:selectManyListbox>
<h:inputText size="30" rendered="#{!myBean.renderList}/>
<h:panelGroup id="listZone">

Resources