How I can change the value of "selectOneMenu" without restart the "inputText"? - ajax

I have the next code:
<h:form id="form" >
<h:panelGrid >
<p:inputText placeholder="Name" value="#{controladorGestionGrados.otherValue}" />
<p:selectOneMenu value="#{controladorGestionGrados.value}" >
<f:selectItem itemValue="A" itemLabel="A" />
<f:selectItem itemValue="B" itemLabel="B" />
<f:selectItem itemValue="C" itemLabel="C" />
<p:ajax update=":form" />
</p:selectOneMenu>
<p:outputLabel id="someText"
value="Some text"
rendered="#{controladorGestionGrados.value eq 'C'}" />
</h:panelGrid>
</h:form>
First: I write anything in the inputText.
Second: I select option C.
After, the outputLabel "Some text" is displayed, but the inputText is reseted.
How I can change the value of "selectOneMenu" without restart the "inputText"?
I've tried:
<p:ajax update="someText" />
But effectively the inputText don't reset, but outLabel don't show.

Wrap your <p:outputLabel>
<p:outputLabel id="someText" value="Some text" rendered="#{controladorGestionGrados.value eq 'C'}" />
with a holder <p:outputPanel>
Like this :
<p:outputPanel id="someTextPanel">
<p:outputLabel id="someTextLabel" value="Some text" rendered="#{controladorGestionGrados.value eq 'C'}" />
</p:outputPanel>
And update the holder component (<p:outputPanel>) with
<p:ajax update="someTextPanel" />
So entire code should be something like this :
<h:form id="form" >
<h:panelGrid >
<p:inputText placeholder="Name" value="#{controladorGestionGrados.otherValue}" />
<p:selectOneMenu value="#{controladorGestionGrados.value}" >
<f:selectItem itemValue="A" itemLabel="A" />
<f:selectItem itemValue="B" itemLabel="B" />
<f:selectItem itemValue="C" itemLabel="C" />
<p:ajax update="someTextPanel" />
</p:selectOneMenu>
<p:outputPanel id="someTextPanel">
<p:outputLabel id="someTextLabel" value="Some text" rendered="#{controladorGestionGrados.value eq 'C'}" />
</p:outputPanel>
</h:panelGrid>
</h:form>

Related

My form inside a dialog keeps values when i re-open dialog

i am using a form inside a dialog to edit some data , i use setPropertyActionListener tag to get this data from a dataTable , my form shows correctly all fields , but when i change some data and close the dialog and reopen it (without submuting) the form keeps the latest data and ignore what is coming from setPropertyActionListener
here is my code used to open the dialog
<p:menuButton value="Actions">
<p:menuitem resetValues="true" value="Update"
icon="fa fa-edit" process="#this"
actionListener="#{myMB.resetSelectedEntite(a)}"
update="formUpdateAff:displayEditionAff"
oncomplete="PF('affUpdateDialogWidget').show()">
<f:setPropertyActionListener
target="#{myMB.selectedAffectation}" value="#{a}" />
<f:actionListener
type="com.dummy.AffActionListener" />
</p:menuitem>
code for dialog :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<p:dialog widgetVar="affUpdateDialogWidget" id="affEditerDialogId"
width="30%" showEffect="explode" hideEffect="explode" modal="true">
<h:form id="formUpdateAff" prependId="true">
<p:panel style="margin-bottom:20px;float:center"
widgetVar="titlePanelWidget" collapsed="true"
styleClass="customTitleBar">
<f:facet name="header">
Update
</f:facet>
</p:panel>
<p:tooltip position="top" />
<h:panelGrid id="displayEditionAff" columns="3" cellpadding="4">
<p:outputLabel value="" />
<h:inputHidden value="#{myMB.selectedAffectation.id}" id="idAff" />
<p:message for="idAff" display="tooltip" />
<p:outputLabel value="Date Aff :" for="dateAffectId" />
<p:calendar value="#{myMB.selectedAffectation.dateAff}"
id="dateAffectId" pattern="dd/MM/yyyy" yearRange="c-10:c+0"
showButtonPanel="true" converter="primefacesCalendarConverter"
required="true" locale="fr" showOn="button" />
<p:message for="dateAffectId" display="tooltip" />
<p:outputLabel value="Ville :" for="villeIdAff" />
<p:selectOneMenu style="width:100%; font-weight:bold"
required="true" id="villeIdAff" appendTo="#this"
value="#{myMB.selectedAffectation.refCompagnie.refGroupement.refVille.id}"
rendered="#{myMB.selectedAffectation.refCompagnie.id != 0}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems value="#{lieuUnitMB.selectLieuxByType('VILLE',0)}"
var="a" itemValue="#{a.id}" itemLabel="#{a.libelle}" />
<p:ajax
listener="#{myMB.loadGroupements(myMB.selectedAffectation.refCompagnie.refGroupement.refVille.id , myMB.selectedAffectation.refCompagnie.refGroupement.refCatGroupement.id)}"
update="groupIdAff groupIdxxx" />
</p:selectOneMenu>
<p:selectOneMenu style="width:100%; font-weight:bold"
rendered="#{myMB.selectedAffectation.refCompagnie.id == 0}"
required="true" id="villeId2" appendTo="#this"
value="#{myMB.selectedAffectation.refGroupement.refVille.id}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems value="#{lieuUnitMB.selectLieuxByType('VILLE',0)}"
var="a" itemValue="#{a.id}" itemLabel="#{a.libelle}" />
<p:ajax
listener="#{myMB.loadGroupements(myMB.selectedAffectation.refGroupement.refVille.id , myMB.selectedAffectation.refGroupement.refCatGroupement.id)}"
update="groupIdAff groupIdxxx" />
</p:selectOneMenu>
<p:message for="villeId2" display="tooltip" />
<p:outputLabel value="Catégorie Groupement :" for="catIdAff" />
<p:selectOneMenu style="width:100%; font-weight:bold"
required="true" id="catIdAff" appendTo="#this"
value="#{myMB.selectedAffectation.refGroupement.refCatGroupement.id}"
rendered="#{myMB.selectedAffectation.refCompagnie.id == 0}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems value="#{repositoryMB.selectRepositories()}" />
<p:ajax
listener="#{myMB.loadGroupements(myMB.selectedAffectation.refGroupement.refVille.id , myMB.selectedAffectation.refGroupement.refCatGroupement.id)}"
update="groupIdAff groupIdxxx" />
</p:selectOneMenu>
<p:selectOneMenu style="width:100%; font-weight:bold"
required="true" id="catId2" appendTo="#this"
value="#{myMB.selectedAffectation.refCompagnie.refGroupement.refCatGroupement.id}"
rendered="#{myMB.selectedAffectation.refCompagnie.id != 0}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems value="#{repositoryMB.selectRepositories()}" />
<p:ajax
listener="#{myMB.loadGroupements(myMB.selectedAffectation.refCompagnie.refGroupement.refVille.id , myMB.selectedAffectation.refCompagnie.refGroupement.refCatGroupement.id)}"
update="groupIdAff groupIdxxx" />
</p:selectOneMenu>
<p:message for="catIdAff" display="tooltip" />
<p:outputLabel value="Groupement :" for="groupIdAff" />
<p:selectOneMenu
value="#{myMB.selectedAffectation.refCompagnie.refGroupement.id}"
style="width:100%; font-weight:bold" required="true"
id="groupIdAff" appendTo="#this"
rendered="#{myMB.selectedAffectation.refCompagnie.id != 0}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems
value="#{myMB.loadGroupements(myMB.selectedAffectation.refCompagnie.refGroupement.refVille.id , myMB.selectedAffectation.refCompagnie.refGroupement.refCatGroupement.id)}" />
<p:ajax
listener="#{myMB.loadCompagniesUpdate(myMB.selectedAffectation.refGroupement.id)}"
update="displayEditionAff,toto" />
</p:selectOneMenu>
<p:selectOneMenu
value="#{myMB.selectedAffectation.refGroupement.id}"
style="width:100%; font-weight:bold" required="true"
id="groupIdxxx" appendTo="#this"
rendered="#{myMB.selectedAffectation.refCompagnie.id == 0}">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems
value="#{myMB.loadGroupements(myMB.selectedAffectation.refGroupement.refVille.id , myMB.selectedAffectation.refGroupement.refCatGroupement.id)}" />
<p:ajax
listener="#{myMB.loadCompagniesUpdate(myMB.selectedAffectation.refGroupement.id)}"
update="displayEditionAff,toto" />
</p:selectOneMenu>
<p:message for="groupIdAff" display="tooltip" />
<p:outputLabel value="Compagnie :" for="toto"
binding="#{myMB.uiOutputLabel}"
rendered="#{not empty myMB.compagniesUpdate}" />
<p:selectOneMenu binding="#{myMB.uiOutput}"
rendered="#{not empty myMB.compagniesUpdate}"
value="#{myMB.selectedAffectation.refCompagnie.id}"
style="width:100%; font-weight:bold" required="false" id="toto"
appendTo="#this" immediate="true">
<f:selectItem itemLabel="----------------" itemValue="" />
<f:selectItems
value="#{myMB.loadCompagniesUpdate(myMB.selectedAffectation.refGroupement.id)}" />
</p:selectOneMenu>
<p:message for="toto" display="tooltip"
rendered="#{not empty myMB.compagniesUpdate}"
binding="#{myMB.uiOutputMsg}" />
</h:panelGrid>
<p:spacer />
<p:spacer />
<h:panelGrid columns="2">
<p:commandButton value="Editer" icon="ui-icon-plus"
action="#{myMB.updateEntite()}"
update=":entiteDetailDialogForm:tabsId:formAff:AffTable :messageGrowl :formUpdateAff"
process=":formUpdateAff"
oncomplete="closeDialogIfSucess(xhr, status, args, 'AffUpdateDialogWidget', 'AffEditerDialogId')" />
<p:commandButton value="#{bundle.cancel}" icon="ui-icon-cancel"
actionListener="#{myMB.resetSelectedEntite()}" type="button"
onclick="PF('AffUpdateDialogWidget').hide();" />
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</html>
i am using Primefaces 6.1

primefaces watermark disappears after disabling and re enabling a text field using p:ajax

I am using p:ajax of primefaces in a p:selectOneRadio, to disable and re-enable form input text fields. these text fields have watermarks which disappear after disabling and they do not appear even after re-enabling them. I could not figure out the problem. please help. The code chunk where it appears is as bellow
<h:outputText
value="#{msg['elicense.contractorLicenseForm.personal.licenseApplied']}"/>
<p:selectOneRadio id="licenseApplied"
value="#{contractorLicenseBean.licenseApplied}">
<f:selectItem itemLabel="Yes" itemValue="1" />
<f:selectItem itemLabel="No" itemValue="0"/>
<p:ajax update="licensePersonName, relationshipPersonName" />
</p:selectOneRadio>
<h:outputText value="When it is yes, it should skip to supervisor details"/>
<h:outputText
value="#{msg['elicense.contractorLicenseForm.personal.licensePersonName']} "/>
<p:inputText id="licensePersonName"
value="#{contractorLicenseBean.licensePersonName}"
required="true"
label="LicensePersonName"
disabled="#{contractorLicenseBean.licenseApplied=='1'}"/>
<p:watermark for="licensePersonName"
value="Person/firm/company on whose Favour License is Sought "
id="watermark2" />
<h:outputText
value="#{msg['elicense.contractorLicenseForm.personal.relationshipPersonName']} "/>
<p:inputText id="relationshipPersonName"
value="#{contractorLicenseBean.relationshipPersonName}"
required="true"
label="RelationshipPersonName"
disabled="#{contractorLicenseBean.licenseApplied=='1'}" />
<p:watermark for="relationshipPersonName"
value="Relationship of the Applicant "
id="watermark3" />
Use a h:panelGroup around your watermarked components and update that h:panelGroup instead of component.
Primefaces binds the watermark to component through JQuery. If you changes/update the component then events bound to that component will not work, that may be the reason for your Issue.
I used primefaces 5.0, and I encountered as your problem. When I put the p:inputText into p:panelGrid and update the panelGrid. The problem was gone.
<h:form>
<p:messages id="messages" showDetail="true"/>
<p:selectOneRadio id="radio" value="#{watermarkView.radio}">
<f:selectItem itemLabel="Yes" itemValue="1" />
<f:selectItem itemLabel="No" itemValue="0"/>
<p:ajax process="radio" update="panelGridInput" />
</p:selectOneRadio>
<h:panelGrid id="panelGridInput"
columns="3" cellpadding="5"
style="margin-bottom:10px">
<h:outputLabel value="Search: "/>
<p:inputText id="keyword"
value="#{watermarkView.keyword}"
required="true"
label="Keyword"
disabled="#{watermarkView.radio eq '1'}"/>
<p:watermark for="keyword"
value="Search with a keyword"
id="watermark" />
<h:outputLabel value="Search: "/>
<p:inputText id="keyword2"
value="#{watermarkView.keyword2}"
required="true"
label="Keyword"
disabled="#{watermarkView.radio eq '1'}"/>
<p:watermark for="keyword2"
value="Search with a keyword"
id="watermark2" />
</h:panelGrid>
<p:commandButton id="regular"
actionListener="#{watermarkView.search}"
value="Regular"
ajax="false" />
<p:commandButton id="ajax"
actionListener="#{watermarkView.search}"
value="Ajax"
onclick="PrimeFaces.cleanWatermarks();"
oncomplete="PrimeFaces.showWatermarks();"
update="messages" />
</h:form>

How to enable my radiobutton with primefaces

enable/disable my selectOneRadio does not work like expected. It only activates after a "submit" and the ajax-direct approach does not work.
When I change the selection (click) to "Group", then the dropDown after the selection keeps being "disabled". It only appears after a "submit", but I like to have direct-ajax function.
<div>
<p:outputPanel id="customPanel" style="margin-bottom:10px">
<p:selectOneRadio id="locationType" value="#{bean.query.locationType}"
required="true" layout="custom">
<f:ajax render="locationGroup locationOrganization">
<f:selectItems value="#{bean.locationTypeSelectItems}"
var="locationTypeItem" itemValue="#{locationTypeItem}" itemLabel="#{msgs[locationTypeItem.label]}" />
</f:ajax>
</p:selectOneRadio>
<h:panelGrid columns="3" cellpadding="5">
<p:radioButton id="option1" for="locationType" itemIndex="0" />
<p:outputLabel for="locationGroup" value="Group"/>
<p:autoComplete id="locationGroup"
value="#{bean.query.locationGroup}"
completeMethod="#{bean.completeGroups}"
var="group"
itemLabel="#{group.description}"
itemValue="#{group}"
converter="org.company.project.presentation.group.GroupConverter"
maxResults="10"
dropdown="true"
cache="true"
disabled="#{bean.locationGroupDisabled}"/>
<p:radioButton id="option2" for="locationType" itemIndex="1" />
<p:outputLabel for="locationOrganization" value="#{msgs.pat_Organization}"/>
<p:autoComplete id="locationOrganization"
value="#{bean.query.locationOrganization}"
completeMethod="#{bean.completeOrganizations}"
var="organization"
itemLabel="#{organization.name}"
itemValue="#{organization}"
converter="org.company.project.presentation.organization.OrganizationConverter"
maxResults="10"
dropdown="true"
cache="true"
disabled="#{bean.locationOrganizationDisabled}"/>
<p:radioButton id="option3" for="locationType" itemIndex="2" />
<p:outputLabel value="All" />
</h:panelGrid>
<p:message for="locationGroup" />
<p:message for="locationOrganization" />
</p:outputPanel>
<p:message for="locationType" />
That's all folks!

PrimeFaces - Validation error in 2nd dropdown (dependent selectOneMenus) - RequestScoped

The 2nd dropdown is being populated by AJAX, but there's validation error in it. Even if the list is populated correctly and a selection is made.
<h:form id="form">
<p:growl id="growl" showDetail="true" sticky="true" globalOnly="true" />
<h:panelGrid columns="3">
<p:outputLabel for="nome" value="Nome:" />
<p:inputText id="nome" value="#{estadoMB.estado.nome}" required="true" validatorMessage="Nome incorreto!" />
<p:message for="nome" />
<p:outputLabel for="sigla" value="Sigla:" />
<p:inputText id="sigla" value="#{estadoMB.estado.sigla}" maxlength="3" size="3" required="true" validatorMessage="Sigla incorreta!" />
<p:message for="sigla" />
<p:outputLabel for="continentes" value="Continente:" />
<p:selectOneMenu id="continentes" value="#{estadoMB.continenteId}" validatorMessage="Selecione um continente!"> <!-- required="# {not empty param[submit.clientId]}" binding="# {menuContinentes}" -->
<f:selectItem itemValue="" itemLabel="-- Selecione --" noSelectionOption="true" />
<f:selectItems value="#{continenteMB.continentes}" var="continente" itemLabel="#{continente.nome}" itemValue="#{continente.id}" />
<p:ajax event="change" listener="#{estadoMB.onPaisesChange}" update="paises" />
</p:selectOneMenu>
<p:message for="continentes" />
<p:outputLabel for="paises" value="País:" />
<p:selectOneMenu id="paises" value="#{estadoMB.pais.id}" validatorMessage="Selecione um país!"> <!-- required="# {not empty param[menuContinentes.clientId]or not empty param[submit.clientId]}" -->
<f:selectItem itemValue="" itemLabel="-- Selecione --" noSelectionOption="true" />
<f:selectItems value="#{estadoMB.paisesCarregados}" var="pais" itemLabel="#{pais.nome}" itemValue="#{pais.id}" />
</p:selectOneMenu>
<p:message for="paises" />
<p:commandButton action="#{estadoMB.save}" value="Salvar" update="#form" /> <!-- binding="# {submit}" -->
</h:panelGrid>
</h:form>
Method onPaisesChange:
public void onPaisesChange() {
if(pais != null) {
Map<String, Object> map = new HashMap<>();
map.put("id", getContinenteId());
paisesCarregados = paisBean.findWithNamedQuery("Pais.findByContinenteId", map);
}
else {
paisesCarregados = new ArrayList<>();
}
}
Already tried EL binding; required="true" in both and I got the same result.
I think it's not possible to do what I want using request scoped beans...

JSF Primefaces form errors after failed validation

I am new to JSF/Java in general but have been trying to solve this for a few days now.
I have a form which gets stuck on validation. When all the entries are valid, it all works well. The minute I break validation (such as don't provide a required value), then the form gets stuck. One thing that happens is I cant correct the invalid entry it seems and repost. Also, where I had a cascading ajax call of a selectOneMenu, that suddenly starts failing...even though this is not the invalidated part of the form.
I notice that the cascading drop down seems to be throwing null pointer exceptions in its overridden equals method.
I am at a loss.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="/templates/layout.xhtml">
<ui:define name="content">
<p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>
<p:dialog modal="true" widgetVar="statusDialog" header="Status"
draggable="false" closable="false">
<p:graphicImage value="/resources/images/ajaxloadingbar.gif" />
</p:dialog>
<h:form id="form">
<p:panel id="panel"
header="Please ensure your personal details are accurate"
style="margin-bottom:10px;">
<p:messages id="msgs" />
<h:outputText
value="Please ensure your personal details are up to date and accurate within the Admin Direct system. Please note that this application only supports GMD customers, so unless you are within GMD, or are a user of the GMD Admin Direct service, please do not enter your details or attempt to use the application for administrative support." />
<h:panelGrid columns="3" cellpadding="5" id="grid">
<h:outputText value="First Name:" style="font-weight:bold" />
<p:inputText id="firstName"
value="#{personalDetailsBean.user.firstName}"
requiredMessage="First name is required..." style="width:250px" />
<p:message id="firstNameMessage" for="firstName" />
<h:outputText value="Last Name:" style="font-weight:bold" />
<p:inputText id="lastName"
value="#{personalDetailsBean.user.lastName}"
requiredMessage="Last name is required..." style="width:250px" />
<p:message id="lastNameMessage" for="lastName" />
<h:outputText value="Select Function:" style="font-weight:bold" />
<p:selectOneMenu id="functions"
value="#{personalDetailsBean.user.function}"
style="width:250px">
<f:selectItem itemLabel="Select Function" itemValue="" />
<f:selectItems value="#{personalDetailsBean.functions}" var="func"
itemLabel="#{func.functionName}" itemValue="#{func}" />
<f:converter converterId="functionConverter" />
</p:selectOneMenu>
<p:message id="functionMessage" for="functions" />
<h:outputText value="Select Site" style="font-weight:bold" />
<p:selectOneMenu id="sites"
value="#{personalDetailsBean.user.site}"
style="width:250px">
<f:selectItem itemLabel="Select Site" itemValue="" />
<f:selectItems value="#{personalDetailsBean.sites}" var="sit"
itemLabel="#{sit.siteName}" itemValue="#{sit}" />
<f:converter converterId="siteConverter" />
<p:ajax update="region" event="change" process="#this"/>
</p:selectOneMenu>
<p:message id="siteMessage" for="sites" />
<h:outputText value="Region" style="font-weight:bold" />
<h:outputText id="region"
value="#{personalDetailsBean.user.site.region.regionName}" />
<p:message id="regionMessage" for="region" />
<h:outputText value="Email" style="font-weight:bold" />
<p:inputText id="email" value="#{personalDetailsBean.user.email}"
required="true"
validatorMessage="Invalid email address format"
style="width:250px">
<f:validateRegex
pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
<f:validateLength minimum="2" />
</p:inputText>
<p:message id="emailMessage" for="email" />
</h:panelGrid>
<p:separator />
<p:commandButton value="Save" id="btnSubmit" actionListener="#{personalDetailsBean.saveUser}" process="#all" update="panel" />
</p:panel>
</h:form>
</ui:define>
</ui:composition>
</html>
any advice greatly appreciated.
Regards
SM

Resources