Search code examples
validationrichfacesjsf-1.2

JSF Validation Error from Component that is not Rendered


I have a form that has a field that needs to be rendered as a read-only value when in edit mode and as a drop-down select list when in create mode.

The read-only field (used in edit mode) is rendered as plain text using <h:outputText>. In create mode, the field is rendered <h:selectOneListbox> that has a required attribute of "true".

It seems to work as I expect most of the time, but occasionally I get a validation error when in edit mode (the select list box is not rendered) .

Here is the code snippet that has both fields defined with their rendered attributes set using the same boolean value (just one field negates the boolean to toggle).

<h:outputLabel id="lblBusinessArea" value="Business Area:" />
<h:panelGroup id="baGroup">
    <h:selectOneListbox id="businessAreaList" size="1"
            rendered="#{shiftDetailsController.canEditBusinessArea}" 
            converter="businessAreaConverter"
            value="#{shiftDetailsController.selectedBusinessArea}"
            label="Business Area"
            required="true">
        <f:selectItems id="businessAreaListItems" value="#{shiftDetailsController.businessAreas}" />
        <a4j:support id="businessAreaA4j" event="onchange"
            ajaxSingle="true" reRender="deploymentGroupList, positionPayGroupList, sapPremCodeList" />
    </h:selectOneListbox>
    <h:outputText id="businessAreaRO" 
         rendered="#{!shiftDetailsController.canEditBusinessArea}" 
         value="#{shiftDetailsController.selectedBusinessArea.busAreaDesc}" />
</h:panelGroup>

Below is a screen clipping showing the field (in edit mode) rendered as read only. The "save" button was clicked and I get a validation error message that the field is required.

  1. The value should be there in the backing bean because the value displayed is from the same object (shiftDetailsController.selectedBusinessArea). The output text uses the description field on the business area ojbect and the select field uses the whole object where the SelectItem has the description in the label.

field rendered as read-only with validation message appearing

Any idea how this could be occurring? I could set the required attribute using the same boolean value that determines the rendered state...so that it is only required when it is rendered...but since this problem is intermittent...I don't know that will really help.

I also verified that no other components on the page inadvertently have a label value of "Business Area" (which is being used in the validation message) to mislead me in which component truly has a null value; this is the only one that has the label of "Business Area".


Solution

  • I figured out what was happening with this issue.

    Prior to submitting the form and receiving the validation error. I was making a change that caused the flag controlling the two components' visibility to be reversed.

    rendered="#{shiftDetailsController.canEditBusinessArea}"
    

    In other words, what I was doing on the form was causing the canEditBusinessArea to change values. But, I was not re-rendering the panel group ("baGroup") to cause the UI to reflect the update.

    Thus, when my form was submitted the component during the validation phase was seen as being required...triggering the validation to happen. As soon as I added "baGroup" to the reRender of the action that was flipping the edit business area flag, the UI began reflecting the update and the submitted form had the value.