I have a selectOneMenu in a form with an ajax to update a separate component when it is triggered. This menu also uses validation. This first item in the menu is null and the validation makes sure that you can't submit the form if this item is null. However, I do want the user to be able to select the null item and that it set the value of that item in my backing bean to null. However the set is not happening when the user selects the null item because the validation failure occurs first and is throwing an exception preventing the value from being set to null in my backing bean.
Here is my code
<h:form>
...
<p:selectOneMenu value="#{myBean.container}">
<f:selectItem itemLabel="Select One" itemValue=#{null} />
<f:selectItems value="#{myBean.getContainerItems()}" />
<p:ajax event="change" update="containerNumSection" />
<f:validateRequired />
</p:selectOneMenu>
...
</h:form>
Is there any way that I can have the value in my backing bean set to null when the user selects the null item while keeping the validation?
I have found a somewhat dirty workaround using a custom validation method that is able to distinct cases where either the selectOneMany is changed or the submit button is pressed:
<h:form>
<p:selectOneMenu id="select" value="#{myBean.stringVal}"
validator="#{myBean.validateSelection}">
<f:selectItem itemLabel="nothing" itemValue="#{null}" />
<f:selectItem itemLabel="a" itemValue="a" />
<f:selectItem itemLabel="b" itemValue="b" />
<p:ajax update="#form" process="#this"/>
</p:selectOneMenu>
<p:commandButton value="submit" update="#form" process="#form">
<f:param name="selectionRequired" value="true"/>
</p:commandButton>
<h:message for="select" />
<h:outputText value="#{myBean.stringVal}" />
</h:form>
If the user presses the submit button, a request parameter named "selectionRequired" is set to "true". If this is the case, the validation method delegates to the javax.faces.validator.RequiredValidator.validate() method, else it simply does nothing.
MyBean.validateSelection:
public void validateSelection(FacesContext context, UIComponent component, Object value) {
String selectionRequired = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap()
.get("selectionRequired");
if (Boolean.valueOf(selectionRequired)) {
new RequiredValidator().validate(context, component, value);
}
}
I had to enable javax.faces.VALIDATE_EMPTY_FIELDS in order for this to work, else the validateSelection method was not invoked at all when #{null} is selected.
web.xml:
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
Related
I have a JSF component which is mapped on a List in the corresponding bean.
As I want the mapped field to contain at least one value when the form is submitted, this field is annoted #NotEmpty.
Bean :
public class MyBean {
#NotEmpty(message="Validation failed")
private List<String> selectedOptions = new ArrayList<>();
... getter & setter
}
Each time the user select something on my component I want the selection to be immediatly mapped in the bean, so I add an ajax behaviour to the component (Here the component is a p:selectManyMenu but the issue seems to be present with any component mapped to a List) :
XHTML :
<h:form>
<p:outputLabel for="optionslist" value="Options :" />
<p:selectManyMenu id="optionslist" value="#{myBean.selectedOptions}" style="margin-bottom:50px">
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
<p:ajax process="#this" update="result optionsListMessage" />
</p:selectManyMenu>
<p:commandButton value="Submit" update="result optionsListMessage" />
<p:message for="optionslist" id="optionsListMessage" />
<p:dataList id="result" value="#{myBean.selectedOptions}" var="option">
<h:outputText value="#{option}" />
</p:dataList>
</h:form>
My issue happens in the following situation :
The user selects one or several choices (Ctrl + click here).
He unselects every choices he just selected.
He submits the form.
We can see in the result dataList that the last unselected value is still in the bean.
My understanding of the situation is that when the user unselects the last value, the validation fails because of the #NotEmpty annotation on the field (as confirmed by the p:message validation failure message). As a consequence the setter is not called and the last unselected value remains in the bean.
How can I, in a proper way, allow the user to unselect every items without validation failures, and run the validator on this field only when the form is submitted ?
This is my current structure for my p:selectOneMenu:
<h:form id="groupSelectionForm">
<p:outputLabel value="Momentane Gruppe:" for="groupSelection" />
<p:selectOneMenu id="groupSelection" value="#{foodPlanManagementBean.selectedGroup}" style="width:150px">
<f:selectItem itemLabel="-" itemValue="#{null}"/>
<f:selectItems value="#{foodPlanManagementBean.getGroups()}" var="group" itemLabel="#{group.name}" itemValue="#{group}"/>
<p:ajax event="change"/>
</p:selectOneMenu>
</h:form>
This results in a checkbox containing a default value given by the single selectItem as well as a few generated options from the selectItems.
However, the setter for the given field "selectedGroup" is only triggering for the selectItem.
The selectItems do not seem to do anything when they are being clicked.
Any ideas?
try to define a listener in ajax component, ex:
<p:ajax id="seasonAjax" event="change" process="#this" listener="#{yourBean.yourMethod}" update="elementThatYouWantToUpdate" />
process = this to process selected element.
In selectItems don't use get method use directly list elements(put get/set in your bean) ex:
<f:selectItems value="#{yourBean.yourList}" var="" itemLabel="" itemValue="" />
If this doesn't work test if you need to use a converter, if selectedGroup is a complex object or pass directly identification of selectGroup( selectedGroup.id)
I hope it helps.
I have a problem when I use a p:tabView with dynamic="true", and there is a h:selectOneMenu on one tab, and on the other is a commandLink which is ajax="false". After clicking to the commandLink twice the value of the selectOneMenu is lost.
This problem does not occur when the tabView is dynamic="false".
The value of the h:inputText is not lost, but I see the following warning in the logfile:
org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils decodeUIInput WARNING: There should always be a submitted value for an input if it is rendered, its form issubmitted, and it was not originally rendered disabled or read-only. You cannot submit a form after disabling an input element via javascript. Consider setting read-only to true instead or resetting the disabled value back to false prior to form submission. Component : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /form/regional/region.xhtml][Class: javax.faces.component.html.HtmlBody,Id: j_id_5][Class: javax.faces.component.html.HtmlForm,Id: TestForm][Class: org.primefaces.component.tabview.TabView,Id: tabviewTest][Class: org.primefaces.component.tabview.Tab,Id: j_id_8][Class: javax.faces.component.html.HtmlInputText,Id: j_id_f]}
Here is the form:
<p:tabView dynamic="true" cache="true" id="tabviewTest">
<p:tab title="Tab 1">
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
</h:selectOneMenu>
<h:inputText value="#{Region.inputValue}" />
</p:tab>
<p:tab title="Tab 2">
<p:commandLink ajax="false"
id="link"
value="Test"
actionListener="#{Region.someActionMethod}" />
</p:tab>
</p:tabView>
And here the Bean:
public class Region {
private Integer dropDownValue = 3;
private String inputValue = "Test";
public void someActionMethod(ActionEvent ev) {
System.out.println("someActionMethod called");
}
public Integer getDropDownValue() {
return dropDownValue;
}
public void setDropDownValue(Integer dropDownValue) {
this.dropDownValue = dropDownValue;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
}
My Environment: Primefaces 5.0/5.1.RC1, Myfaces 2.1/2.2, Tomact 7
Any ideas what could be wrong?
What scope does your ManagedBean have?
When you use a RequestScope you are not able to submit your selectOneMenu with an UICommand component like p:commandLink when you set the ajax attribute to false. The changes are lost in this case.
Here are two possibilities to fix your problem:
Attempt 1: Set your Bean ViewScoped:
In most cases this will work. If you must use special annotations to annotate your beans (like Apache DeltaSpike #ViewAccessScoped for example), try to separate your bean into View and Controller beans, annotating the View with just simple #ViewScope and keeping all the values in it.
Attempt 2: Remove ajax="false" from p:commandLink:
This will work if your use-case allows it. For example, downloading a file with PrimeFaces will require explicit declaration that the ajax is not to be used, so this solution will not be applicable.
add ajax listener
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
<p:ajax event="change" update="#this"/>
</h:selectOneMenu>
Here is my jsp:
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" immediate="true" />
<h:inputText value="#{member.dob}" immediate="true" />
And this, the function valuechangelistener fires.
public void getDependentAddress(ValueChangeEvent e) {
setPersonName((getDependentsList().get(e.getNewValue().toString())
.getDependentName()));
setDob(getDependentsList().get(e.getNewValue().toString()).getBirth());
System.out.println("New dob value : " + dob);
System.out.println("New name value : " + personName);
FacesContext.getCurrentInstance().renderResponse();
}
The two sysouts give the new value in the console but once the page loads, the fields are blank. I have tried all scopes for the bean. No go. What am i missing?
Thanks
You missed nothing. You've just something too much. To get it to work, you should remove immediate="true" from the to-be-changed components.
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" />
<h:inputText value="#{member.dob}" />
The immediate="true" on an UIInput component will cause its validations phase to take place in apply request values phase instead. This gives you the opportunity to use FacesContext#responseComplete() inside a valueChangeListener method skip other components which doesn't have immediate="true" set from being processed. As you have now, with immediate="true", they are also processed.
Please note that this is essentially a hack from the old JSF 1.x ages. If you're already using JSF 2.x, you should be using <f:ajax listener> instead.
<h:selectOneMenu value="#{member.dependentName}">
<f:selectItems value="#{member.dependentList}" />
<f:ajax listener="#{member.getDependentAddress}" render="name dob" />
</h:selectOneMenu>
<h:inputText id="name" value="#{member.personName}" />
<h:inputText id="dob" value="#{member.dob}" />
with
public void getDependentAddress() {
Dependent dependent = getDependentsList().get(dependentName); // Isn't that actually a Map instead of List?
personName = dependent.getDependentName();
dob = dependent.getBirth();
}
The problem occurs with this code:
<h:form>
<rich:panel>
<f:facet name="header">
<h:selectManyCheckbox title="Select which types of requests you want to see"
onchange="submit();" value="#{filterListener.chosenFilters}"
id="selectBoxContainer" >
<f:selectItem id="approvedByITS" itemLabel="Approved by ITS" itemValue="approvedByITS" />
<f:selectItem id="approvedByPO" itemLabel="Approved by Process Owner" itemValue="approvedByPO" />
<f:selectItem id="dob" itemLabel="Date" itemValue="dob" />
<f:selectItem id="externalAssignedTo" itemLabel="External assigned" itemValue="externalAssignedTo" />
<f:selectItem id="internalAssignedTo" itemLabel="Internal assigned" itemValue="internalAssignedTo" />
<f:selectItem id="ITSapprovedBy" itemLabel="ITS approved by" itemValue="ITSapprovedBy" />
<f:selectItem id="severity" itemLabel="Severity" itemValue="severity" />
<f:selectItem id="status" itemLabel="status" itemValue="status" />
<f:valueChangeListener type="no.ngt.tech.rt2.listeners.requestFilterListener" />
</h:selectManyCheckbox>
</f:facet>
<h:dataTable value="#{filterListener.chosenFilters}" var="selects" >
<h:column>
<h:outputText value="#{selects}" />
</h:column>
</h:dataTable>
<br />
<h:messages />
</rich:panel>
</h:form>
As we can see I have the value="#{filterListener.chosenFilters}". The dataTable's value is also the same, so whenever I click one of the selectItem's the dataTable has an element added or removed from it (this is working). In my backing bean I have the following code:
public class requestFilterListener implements ValueChangeListener {
private List<String> chosenFilters;
public requestFilterListener() {
}
public void processValueChange(ValueChangeEvent event) {
System.out.println("processValueChange called");
if (chosenFilters != null) {
System.out.println(chosenFilters.size());
}
}
public List<String> getChosenFilters() {
return chosenFilters;
}
public void setChosenFilters(List<String> chosenFilters) {
this.chosenFilters = chosenFilters;
}
Everytime I click one of the checkboxes, a column is added/removed with the proper data, in my console I get the message "processValueChange called" as I output in the processValueChange method, but during this time chosenFilters is always null, and the if expression is never run. How come? This is a session bean, and I really dont understand why my list cant be used within the backing bean, but is used without a problem by my dataTable.
Thanks for your time in looking into this.
The problem is probably on this tag:
<f:valueChangeListener type="no.ngt.tech.rt2.listeners.requestFilterListener" />
You are instructing the f:valueChangeListener tag to create a new instance of requestFilterListener instead of binding to the one specified by the managed bean configuration. Use the binding attribute to bind to #{filterListener}.