How to make an element conditionally required in JSF [duplicate] - jsf

This question already has an answer here:
Skip required validation and invoke the application
(1 answer)
Closed 8 years ago.
I have a simple input text element in my form.
The user has 2 possible actions to take on that form: 1) approve the form and 2) reject the form. So there are 2 buttons at the bottom of the form for the user to click on.
If the user clicks on approve the comment inputField should be optional. If the user clicks on reject the comment inputField should be required.
Here is a simple representation of the form:
<h:form>
<p:inputText id="comment" required="???" .../>
<p:commandButton id="approve" ... />
<p:commandButton id="reject" ... />
</h:form>

Try this.
<h:form id="forma">
<p:inputText id="comment"
value="#{userView.firstname}"
title="comment"
required="#{not empty param[reject.clientId]}"/>
<p:message for="comment"/>
<p:commandButton id="approve"
binding="#{approve}"
value="approve"
process="#form"
update="#form"/>
<p:commandButton id="reject"
binding="#{reject}"
value="reject"
process="#form"
update="#form"/>
</h:form>

Your question looks similar to Dynamic required validation for different buttons?.
Modify the required attribute as follows:
<h:form id="form">
<p:inputText id="comment" required="#{not empty param['form:reject']}" ... />
<p:commandButton id="approve" ... />
<p:commandButton id="reject" ... />
</h:form>

Related

Using temporary value without backing bean [duplicate]

This question already has answers here:
Pass input text value to bean method without binding input value to bean property
(2 answers)
Closed 6 years ago.
Inside a primefaces overlayPanel I am asking a user for a value, which will be reused directly afterwards. So I don't want to save it in a backing bean.
Here's the code:
<p:overlayPanel id="ovlpanel" for="copy" hideEffect="fade" style="width:300px">
<h:form>
<p:panel id="grid" header="#{msg.copy_item}">
<h:panelGrid columns="2" cellpadding="5"
styleClass="ui-panelgrid-blank">
<p:outputLabel value="#{msg.number_of_copies}: " for="number_of_copies" />
<p:column>
<p:inputText id="number_of_copies"
value="tempValNumberOfCopies" />
</p:column>
</h:panelGrid>
<p:commandButton
action="#{controller.copyItem(tempValNumberOfCopies)}"
value="#{msg.copy}" />
</p:panel>
</h:form>
</p:overlayPanel>
I want to save the value for tempValNumberOfCopies inside a temporary variable and use it in the action of the button.
Do you maybe have any ideas how to implement that?
You could use the implicit scopes, e.g.
<p:inputText id="number_of_copies"
value="requestScope.tempValNumberOfCopies" />
<p:commandButton action="#{controller.copyItem(requestScope.tempValNumberOfCopies)}"
value="#{msg.copy}" />
or
<p:inputText id="number_of_copies"
value="viewScope.tempValNumberOfCopies" />
<p:commandButton action="#{controller.copyItem(viewScope.tempValNumberOfCopies)}"
value="#{msg.copy}" />

Why does a primefaces view keep a value when I´m setting it as new? [duplicate]

This question already has an answer here:
Dialog not updating after populating model in action method
(1 answer)
Closed 6 years ago.
I´m just trying to get some object when I do double click over a data table row and show it on a modal dialog. I should be missing something because it does it correctly only one time. After the bean object is setted, it keeps the value no matter what I do.
Here is the code:
*.xhtml:
<!-- Modal dialog -->
<p:dialog widgetVar="addNotDlg" id="idAddNotDlb" minHeight="40" modal="true"
resizable="false" draggable="false" style="heigth:500">
<p:panel id="panel">
<p:messages id="messages" />
<h:panelGrid columns="3" cellpadding="5">
<p:outputLabel for="resultado" value="Resultado:" />
<p:inputText id="resultado"
value="#{acuerdoBean.notificacion.resultado}" label="resultado" />
<p:message for="resultado" />
<p:outputLabel for="motivo" value="Motivo:" />
<p:inputText id="motivo"
value="#{acuerdoBean.notificacion.motivo}" label="motivo" />
<p:message for="motivo" />
</h:panelGrid>
<p:commandButton value="Guardar" id="addNewNotButton" actionListener="#{acuerdoBean.addNotificacion()}"
update=":formNotificacion:notificacionesTable"
oncomplete="handleLoginRequest(xhr, status, args)" />
<p:commandButton value="Cerrar" id="closeNewNotButton" onclick="PF('addNotDlg').hide();" />
<h:outputText value="" />
</p:panel>
</p:dialog>
*Bean.java code:
public void generateNewNotificacion() {
this.tiposNotificaciones = pservice.getTiposNotificaciones();
this.tipoNotificacion = new Rstiponotifica();
this.direccionPorDefecto = (Rsdireccion) pservice.findDireccionPorDefecto();
this.notificacion = new Rsnotificacion();
this.notificacion.setRsacuerdo(acuerdo);
this.notificacion.setRstiponotifica(tipoNotificacion);
this.notificacion.setRsdireccion(direccionPorDefecto);
}
The above function is executed in other .xhtml file event. I would expect that my "this.notificacion" object should appear as new in the modal, but it doesn´t. The "this.notification" object was setted previously and it remains in memory.
<p:commandLink onclick="PF('addNotDlg').show();" actionListener="#{acuerdoBean.generateNewNotificacion()}">
<h:outputText value="Añadir nueva notificacion" />
</p:commandLink>
Thanks in advance.
Updating your panel inside the dialog when you click the command link should fix your problem. The commandLink have an update attribute that you can use.
You can see a working example on primefaces website : http://www.primefaces.org/showcase/ui/data/schedule.xhtml

Managing Required=true in JSF [duplicate]

This question already has an answer here:
Temporarily disable JSF validations
(1 answer)
Closed 6 years ago.
I have two panelgrids in my page.
In the first panel grid I have two input text with a button. This
button should valid only this input text.
In the second panelgrid i have 3 input text with a button which
should validate these 3 text only.
How can I ensure that on button click in thr first panelgrid , the
required=true gets ignored for all elements in the second panel grid.
Any help would be appreciated.
If you are using Primefaces you can use partial submit.
For your example:
<h:form>
<p:panelGrid id="grid1">
<p:inputText required="true" id="inp1" value="#{test.value1}"/>
<p:inputText required="true" id="inp2" value="#{test.value2}"/>
<p:commandButton partialSubmit="true" process="#this inp1 inp2" action="#{test.someAction()}" value="validation" update="grid1"/>
</p:panelGrid>
<p:panelGrid id="grid2">
<p:inputText required="true" id="inp3" value="#{test.value3}"/>
<p:inputText required="true" id="inp4" value="#{test.value4}"/>
<p:inputText required="true" id="inp5" value="#{test.value5}"/>
<p:commandButton partialSubmit="true" process="#this inp3 inp4 inp5" action="#{test.someAction()}" value="validation" update="grid2"/>
</p:panelGrid>
</h:form>
Or you can palce grids with buttons in separate forms. Beacuse by default ajax request posts (and validates) values only from current form.
<h:form>
<p:panelGrid id="grid1">
<p:inputText required="true" id="inp1" value="#{test.value1}"/>
<p:inputText required="true" id="inp2" value="#{test.value2}"/>
<p:commandButton action="#{test.someAction()}" value="validation" update="#form"/>
</p:panelGrid>
</h:form>
<h:form>
<p:panelGrid>
<p:inputText required="true" id="inp3" value="#{test.value3}"/>
<p:inputText required="true" id="inp4" value="#{test.value4}"/>
<p:inputText required="true" id="inp5" value="#{test.value5}"/>
<p:commandButton action="#{test.someAction()}" value="validation" update="#form"/>
</p:panelGrid>
</h:form>

How to set JSF input field value before submit

I a have a JSF page with PrimeFaces with some input fields. Before a submit is done, I would like to use the value of a field as input for a method which does some calculations and updates another field with the result but without submitting the form.
This is the field that will be used as input for my method:
<h:outputLabel for="hiredate" value="#{msgs['addUser.hireDate']}" />
<p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" immediate="true"/>
<p:message for="hiredate" />
The calculation is done by clicking a <p:commandButton>:
<p:commandButton value="Calculate days" icon="ui-icon-circle-check" action="#{userWizardMB.calculateVacationDays}" update="vacationDays" process="#this" immediate="true"/>
And this is the method called:
public void calculateVacationDays() {
user.setVacationDays((int) vacationDaysCalculator
.calculateWithHireDate(user.getHireDate()));
}
When debugging, though, I see that this field is NULL even if I set value in the form.
How can I force the setting of this field - user.hireDate because I really need this value for my calculation?
Thank you
Edit: I removed all of the other fields in the form and the immediate attribute:
<h:form id="addUserForm">
<p:wizard widgetVar="wizard" flowListener="#{userWizardMB.onFlowProcess}">
<!-- TAB FOR PERSONAL DATA -->
<p:tab id="personal" title="#{msgs['addUser.personalTab']}">
<p:panel header="#{msgs['addUser.personalInformation']}">
<p:message for="vacationDays" showDetail="true" autoUpdate="true" closable="true"/>
<h:panelGrid columns="3" columnClasses="label, value" styleClass="grid">
<h:outputLabel for="hiredate" value="#{msgs['addUser.hireDate']}" />
<p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" />
<p:message for="hiredate" />
<h:outputLabel for="vacationDays" value="#{msgs['addUser.vacationDays']}"/>
<p:inputText id="vacationDays" value="#{userWizardMB.user.vacationDays}"/>
<p:commandButton value="Calculate days" icon="ui-icon-circle-check" action="#{userWizardMB.calculateVacationDays}" process="#this hiredate" update="vacationDays"/>
</h:panelGrid>
</p:panel>
</p:tab>
</p:wizard>
</h:form>
And the backing bean method is still not called.
Remove immediate="true" from the input and the command component. Do not use immediate unless you really understand what it should be used for. Further you also need to include the input component which you'd like to process in the update attribute of the command component. Note that this should represent the client ID, not the property name as mentioned in one of your comments.
<p:calendar id="hiredate" value="#{userWizardMB.user.hireDate}" required="true" />
...
<p:commandButton value="Calculate days" icon="ui-icon-circle-check"
action="#{userWizardMB.calculateVacationDays}"
process="#this hiredate" update="vacationDays" />
See also:
Why was "immediate" attribute added to the EditableValueHolders?
Use process="#form" (or process="[the id of the calendar component]" in the commandButton
process="#this" means that only the part of the model related to the commandButton (usually none) gets updated.
Old question, but did you add partialSubmit="true" in the commandButton tag? At least in PrimeFaces 3.5, false is the default value of this attribute (see the PrimeFaces PDF documentation).

How to only show dialog after successfully validated text field

So I have form with 1 fields that need to be required
<h:form id="form">
<h:panelGrid columns="2">
<h:inputText id="default" requiredMessage="This field is required."
value="#{displayName.name}" required="true" />
<p:message for="default"/>
</h:panelGrid>
<p:commandButton value="Test" update="form" onclick="displayDlg.show();"/>
</h:form>
<p:dialog header="Dialog" widgetVar="displayDlg" id="dialog">
<h:outputText value="The name you just input is: #{displayName.name}"/>
</p:dialog>
So when I click button Test, and not having anything inside the text-box, then an error message pop out.
I want that if the validation process success, then I want to display a dialog box that display name that you just type in. But if validation fail, then dont diplay the dialog. And I have a hard time making this happen. I try to put the dialog inside or outside the dialog, but no work. Please give me some advice
You could make use of the visible attribute of <p:dialog> and ajax-update the whole dialog.
<h:form id="form">
<h:panelGrid columns="2">
<h:inputText id="default" requiredMessage="This field is required."
value="#{displayName.name}" required="true" />
<p:message for="default"/>
</h:panelGrid>
<p:commandButton value="Test" update="form :dialog" />
</h:form>
<p:dialog id="dialog" header="Dialog" visible="#{not empty displayName.name}">
<h:outputText value="The name you just input is: #{displayName.name}"/>
</p:dialog>
Additional benefit of having :dialog in the update is that it will update the entered name as well everytime you change it.

Resources