Question

Versions :

Aapche MyFaces 2.1.14 RichFaces 4.3.5

Issue :

I am facing very strange issue for JSF2.1

As shown in code snippet at the end , boolean check box is disabled based on boolean variable #{bean.disabled}. The issue is below :

1)First #{bean.disabled} evaluates to false so checkbox is enabled. User clicks the checkbox and submits the form (form is not shown here )

2)The action method sets the disabled = true and required= true and same page is rendered again

3)At this stage , UI has check box clicked and disabled. When at this stage , form is submitted again , bean.setRequired method is called with setter value as FALSE whereas it should set it with value as TRUE

4)When I made disabled="#{false}" , this issue vanished and bean.setRequired method is called with setter value as TRUE

So question is why is the behaviour in step 3 observed even though UI is showing check box in clicked condition (meaning it has a value as TRUE , i also printed the bean.required value and is evaluating to TRUE only

Code :

        <h:panelGroup>

            <h:selectBooleanCheckbox id="invite" disabled="#{bean.disabled}" value="#{bean.required}" />

                <h:outputText value="Do Update"/>
         </h:panelGroup>    
Was it helpful?

Solution

Make sure the backing property bound to #{bean.disabled} is a boolean and not a string for which values "TRUE" and "FALSE" are being set.

Also, the behavior you described in step 3 is expected. When the checkbox is disabled, the value submitted will always be false since browsers normally ignore disabled fields during form submit. To make sure you get the right value, maintain a hidden field alongside the checkbox, keep both of them in sync and use the value of the hidden field instead to test if the checkbox is checked or unchecked. Something like below:

<h:selectBooleanCheckbox id="invite" disabled="#{bean.disabled}" value="#{bean.required}" />
<h:inputHidden id="inviteHidden" value="#{bean.requiredHidden}" />

// While setting values, set values for both fields inside the action method

bean.setRequired(true); // ..or false
bean.setRequiredHidden(true); // ..or false

// And to test whether checkbox is checked, do...
if(bean.isRequiredHidden()) {
   ....
}

Unrelated to your question, if you're using the <c:set/> for conditional rendering, consider using <a4j:outputPanel rendered = "#{someCondition}"/> instead and on your form submit update it (if it is an AJAX submit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top