I have a <h:selectOneMenu>
. Depending on what is selected will be shown one of the many <div>
s related to the selection and will be hidden the others.
Each <div>
has some <h:inputText>
that write to different @ViewScoped
beans. Some of this <div>
s even write to the same properties in the beans.
Ex.
<div>
<h:outputLabel for="list" value="Items"/>
<div>
<h:message for="list"/>
<h:selectOneMenu id="list" value="#{bean.selectedItem}" >
<f:selectItem itemLabel="Select one"></f:selectItem>
<f:selectItems value="bean.someItemsList" />
</h:selectOneMenu>
</div>
</div>
<div id="item1">
<!-- some other input fields -->
<div>
<h:message for="item1input1"/>
<h:inputText id="item3input1" value="bean.thisIsTheSameProperty" />
</div>
</div>
<div id="item2">
<!-- some other input fields -->
</div>
<div id="item3">
<!-- some other input fields -->
<div>
<h:message for="item3input1"/>
<h:inputText id="item3input1" value="bean.thisIsTheSameProperty" />
</div>
</div>
The problem: When I select an item that will display a <div>
(ex. <div id="item1">
) and there is also another hidden <div>
(ex. <div id="item3">
) that writes to the same bean properties (ex. value="bean.thisIsTheSameProperty"
) and this properties are annotated with javax.validation.constraints.@NotNull
, even I give a value to this input fields, when I submit the form, I think JSF runs also the hidden <div>
(which normally has no input set).
What I see during debugging: When the form will be submited I see the setter of the bean will be called twice. The first time the bean properties will be set with the correct valeus I typed in but the second time the setter will be called with null values. So the validation will fail because of the @NotNull.
My assumption is that JSF tries to set the bean values twice, one for the input fields on the shown <div>
and the second time for the hidden <div>
(because they point to the same bean properties), but for the hidden bean there are not input fields set (they are null).
I show/hide the <div>
s with jQuery depending on the item selected from the <h:selectOneMenu>
.
Ex.
$('#item1').show();
$('#item1').hide();
$('#item2').show();
$('#item2').hide();
$('#item3').show();
$('#item3').hide();
Is there a way to say JSF to not consider the hidden <div>
s at all?