This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to perform JSF validation in actionListener or action method?
(5 answers)
JSF doesn't support cross-field validation, is there a workaround?
(2 answers)
Closed 2 years ago.
I spent some days to try to validate part form with a OneSelect like as "Terms and Conditions", for example If are pre-filled Name and Telephone and I selected "Terms and Conditions" in this moment validate the previous fields are filled and enable "Next" button.
I tried using two ways, the first I put p:attribute inside p:selectOneRadio and received in method validateSelection() always null, the second is used validateSelection(String name, String telephone), but both methods show null values,
Does anyone know why this happens?
Thanks a lot.
(this is my view)
<h:form id="frmUser" enctype="multipart/form-data">
<p:outputLabel id="lblName" for="txtName" value="#{msg.lbl_name}" styleClass="lbl8"/>
<p:inputText id="txtName" maxlength="7" required="true" value="#{mainBean.userDTO.name}"/>
<p:message for="txtName" display="text"/>
<p:outputLabel id="lblTel" for="lblTel" value="#{msg.lbl_tele}" />
<p:inputText id="lblTel" maxlength="7" required="true" value="#{mainBean.userDTO.telephone}"/>
<p:message for="lblTel" display="text"/>
<p:outputLabel for="console" value="Accept Terms and Conditions" />
<p:selectOneRadio id="console" unselectable="true" value="#{mainBean.selectTerms}">
<f:selectItem itemLabel="Yes" itemValue="true" />
<f:selectItem itemLabel="No" itemValue="false" />
<f:attribute name="setname" value="#{mainBean.userDTO.name}" />
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection}" />
<!--WAY TWO -- >
<!--
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection(mainBean.userDTO.name,mainBean.userDTO.telephone)}" />
-->
</p:selectOneRadio>
</h:form>
This is my Bean
#Named("mainBean")
#SessionScoped
public class mainBean implements Serializable{
private UserDTO userDTO;
public String init() {
userDTO = new UserDTO();
//call url to display
}
/*
* Getters y Setters
* */
public UserDTO getUserDTO() {
return userDTO;
}
public void setUserDTO(UserDTO userDTO) {
this.userDTO = userDTO;
}
public void validateSelection(AjaxBehaviorEvent event) {
System.out.println("fill name"+ userDTO.getName());
System.out.println("fill name in ajax event :"+event.getComponent().getAttributes().get("setname"));
UIComponent component = event.getComponent();
System.out.println("fill name in ajax event component:"+
component.getAttributes().get("setname"));
//If two values are filled, eneabled NEXT BUTTON
}
public void validateSelection(String name, String telephone){
System.out.println("fill name"+name);
System.out.println("fill telephone"+telephone);
//If two values are filled, eneabled NEXT BUTTON
}
}
And this is the UserDTO class
public class UserDTO implements Serializable{
private String name;
private String telephone;
public UserDTO(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
Related
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
(2 answers)
Closed 2 years ago.
I'm working with JSF and want to save a choose from a dropdown, but it doesn't save it, what can I do?
xhtml:
<h:form>
<h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}" >
<f:selectItems value="#{spielController.alleKategorien}"/>
</h:selectOneMenu>
</h:form>
<br/>
<h:form>
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form>
BackingBean:
#Named(value = "spielController")
#SessionScoped
public class SpielController implements Serializable {
private String ausgewaehlteKategorie;
private ArrayList<String> alleKategorien = new ArrayList<>();
public SpielController() throws SQLException {
for (String kategorie : kdao.getKategorien()) {
alleKategorien.add(kategorie);
}
}
public String getAusgewaehlteKategorie() {
return ausgewaehlteKategorie;
}
public void setAusgewaehlteKategorie(String ausgewaehlteKategorie) {
this.ausgewaehlteKategorie = ausgewaehlteKategorie;
}
public ArrayList<String> getAlleKategorien() {
return alleKategorien;
}
public void setAlleKategorien(ArrayList<String> alleKategorien) {
this.alleKategorien = alleKategorien;
}
}
I get the things from a DAO-class and it works, but when I press the button, it doesn't work because the choose I made above wasn't saved
Move
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
to h:form
<h:form>
<h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}" >
<f:selectItems value="#{spielController.alleKategorien}"/>
</h:selectOneMenu>
<br/>
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form>
This is the reason why your setter not invoked.
I need show a value in an dialog after execute a method that I invoke in the commandbutton but the dialog don't show the variable value.
The variable value is calculate inside the commandbutton.
Managed bean
#ManagedBean
#ViewScoped
public class OrderBean implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void eventicus(ActionEvent event) {
this.name ="Value";
}
}
xhtml document
<h:commandButton actionListener="#{orderBean.calculate()}" value="Show" oncomplete="PF('dlg2').show();" />
<p:dialog id="modalDialog" header="Modal Dialog" widgetVar="dlg2" >
<h:panelGrid columns="2" style="margin-bottom:10px">
<h4><h:outputText value="#{orderBean.name}" /></h4>
</h:panelGrid>
<p:commandButton id="submitButton" value="Submit" oncomplete="PF('dlg2').hide();"/>
</p:dialog>
When I click in the commandbutton the dialog is empty.
1 I think you need to add a [update="modalDialog"] attribute inside your commandButton
Form field rendering is depending on selected item in selectOneMenu.
Page:
<h:body>
<f:view>
<h:form>
<h:panelGrid>
<p:inputText value="#{user.username}"/>
<p:selectOneMenu value="#{user.moreInputs}"
required="true">
<p:ajax event="change"
update="moreInputGrid"/>
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItems value="#{user.selectItems}"/>
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid id="moreInputGrid">
<p:inputText rendered="#{user.renderMoreInputs}"
value="#{user.name}"/>
</h:panelGrid>
<p:commandButton action="#{user.register}"
value="Register user"/>
</h:form>
</f:view>
</h:body>
Backing bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
#ManagedBean
#ViewScoped
public class User {
private String username;
private MoreInputs moreInputs;
private String name;
public enum MoreInputs {
YES,
NO
}
public boolean isRenderMoreInputs() {
return (moreInputs == MoreInputs.YES);
}
public SelectItem[] getSelectItems() {
SelectItem[] items = new SelectItem[2];
items[0] = new SelectItem(
MoreInputs.YES,
"yes");
items[1] = new SelectItem(
MoreInputs.NO,
"no");
return items;
}
public String register() {
return null;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public MoreInputs getMoreInputs() {
return moreInputs;
}
public void setMoreInputs(MoreInputs moreInputs) {
this.moreInputs = moreInputs;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Issue arrises if the client does page refresh after choosing an item causing form fields to render. Such form fields will not be rendered on page refresh, although they should. Plus, if client then tries to submit form, validation is skipped for those hidden fields and form is successfully processed.
Am I doing something wrong? Is there an elegant solution?
i have a sample selectOneMenu that have List of date and date as values but when i try to validate i have it red i will show you my sample example :
my managed bean :
#ManagedBean
#SessionScoped
public class Testbean {
#EJB
private ManageOfPlanifieLocal manageOfPlanifie;
List<Date> listdate = new ArrayList<Date>();
Date newdate;
#PostConstruct
public void initialize() {
listdate=manageOfPlanifie.retournerdatedesplanif();;
}
public String gototest2(Date date)
{
return "test2.xhtml?faces-redirect=true";
}
public List<Date> getListdate() {
return listdate;
}
public void setListdate(List<Date> listdate) {
this.listdate = listdate;
}
public Date getNewdate() {
return newdate;
}
public void setNewdate(Date newdate) {
this.newdate = newdate;
}
}
and this is my two jsf pages :
test1.xhtml
<h:outputLabel for="dateplanif" value="date de planification : " />
<p:selectOneMenu id="dateplanif" value="#{ testbean.newdate}">
<f:selectItems value="#{testbean.listdate}" var="da" itemValue="#{da}" />
</p:selectOneMenu>
<p:commandButton value="suivant" style="color:black;" action="#{testbean.gototest2(testbean.listdate)}" update="#form" />
test2.xhtml
<h2>Choix de l'equipe</h2>
<h:outputText value="Date : "/>
<h:outputText value="#{ testbean.newdate}"/>
the problem i do sample transfer of data with out converstion just simple and i get that :
do you know i have it red and i cant move to the next page ??
When you have a list of Objects, you need to convert them, so that setting the value works properly. Try the following code.
<p:selectOneMenu id="dateplanif" value="#{testbean.newdate}">
<f:selectItems value="#{testbean.listdate}" var="da" itemValue="#{da}" />
<f:convertDateTime pattern="dd-MM-yyyy" />
</p:selectOneMenu>
it seems as if it is not possible to disable items in a SelectOneMenu from PrimeFaces (3.3) when using the custom content (as shown at http://www.primefaces.org/showcase-labs/ui/selectOneMenu.jsf).
The testcase is simple, just take the following:
<h:form>
<p:selectOneMenu value="#{testBean.selected}">
<f:selectItems value="#{testBean.options}" var="t"
itemLabel="#{t.label}" itemValue="#{t}"
itemDisabled="#{t.value % 2 == 0 ? 'true' : 'false'}" />
</p:selectOneMenu>
<p:selectOneMenu value="#{testBean.selected}" var="x">
<f:selectItems value="#{testBean.options}" var="t"
itemLabel="#{t.label}" itemValue="#{t}"
itemDisabled="#{t.value % 2 == 0 ? 'true' : 'false'}" />
<p:column>
#{x.label} -- #{x.label}
</p:column>
</p:selectOneMenu>
</h:form>
and the following Java files:
Bean:
#Named
public class TestBean {
private TestObject selected;
// getter/setter
public List<TestObject> getOptions() {
return Arrays.asList(new TestObject("1"), new TestObject("2")); }
}
Object:
public class TestObject {
private Integer value;
// getter/setter
public TestObject() {}
public TestObject(String s) {
this.setLabel(s);
}
public String getLabel() {return "label: " + value;}
public void setLabel(String l) {this.value = new Integer(l);}
}
The first dropdown works fine, the second one doesn't. Any idea on how to solve that?