0

I have a check-box that disables some form.

<h:selectBooleanCheckbox id="ldapAuthEnabled" value="#{enabled}" label="check it">

When I check it - it lead to disable all field in my form, so that:

<h:inputText mandatory="true" value="#{someVar}" 
disabled="#{!enabled}" required="#{enabled}" />

The problem comes up when

  1. I try to safe my form when fields are not disabled - it shows me validation messages!
  2. Then I click on the check-box, it makes all fields disabled, BUT - it still show me validation messages! So, It even can not go to my controller.safe() method because of this.

The question is: how to make it work as expected? - when fields are disabled, no need to keep validation messages and think that form is not valid.

-- EDIT:

As suggested in the answer (from BalusC), I changed "value" to "binding", so now I have code like this:

<h:panelGroup id="my-panel">

<h:selectBooleanCheckbox id="myEdit" binding="#{checkbox}" label="enable it!">
  <f:ajax event="click" render="my-panel"/>
</h:selectBooleanCheckbox>

<h:inputText id="name" value="#{bean.name}" disabled="#{not checkbox.value}" required="true" />
   <h:outputText value="sometext"/>                      
</h:panelGroup>

Where "<h:panelGroup id="my-panel">" is part of the "form".

But have the same result - validation error that prevent me to do save().

I use "SessionScope" for my bean now.

ses
  • 13,174
  • 31
  • 123
  • 226
  • Is this real code? I'm not seeing any value bindings to a model. E.g. `#{bean.enabled}`. Please post code in SSCCE flavor. So far, the symptoms suggest that the condition is been stored in request scope instead of view scope and/or that JSF ajax is not been utilitzed to update the form. – BalusC Aug 28 '13 at 20:01
  • Yes, I though about request scope as well. But in most cases it is quite useful to keep form values in session scope to make it safe when user refresh the page.. (about a code: I just simplified it a little to put here. but it is real) – ses Aug 28 '13 at 20:20
  • Okay, but in the future please learn how to create an SSCCE. The code must be copy'n'paste'n'runnable (by yourself!!) without modifications when copypasted into `` of a completely blank non-templated page in order to reproduce/demonstrate the desired problem. See also http://stackoverflow.com/tags/jsf/info – BalusC Aug 28 '13 at 20:41

1 Answers1

3

The concrete functional requirement is sound, but the code posted so far is so strange and incomplete that it's hard to explain the concrete problem with it. What you've so far does theoretically not work because the model values are only updated during update model values phase, while the disabled/required attributes are checked long before during apply request values and validations phases.

Essentially, you should be binding the checkbox value to a boolean property in a model which lives in the view scope, like so:

<h:selectBooleanCheckbox value="#{bean.enabled}" />
<h:inputText required="true" disabled="#{not bean.enabled}" />

or at least be checking the physical checkbox component itself to the view like so:

<h:selectBooleanCheckbox binding="#{checkbox}" />
<h:inputText required="true" disabled="#{not checkbox.value}" />

Don't forget to submit the value and ajax-update the form when the checkbox is (un)checked. I'm not seeing that anywhere in your attempt either. All in all, this complete copy'n'paste'n'runnable kickoff example should get you started:

<h:form>
    <h:selectBooleanCheckbox binding="#{checkbox}">
        <f:ajax render="@form" />
    </h:selectBooleanCheckbox>

    <h:inputText required="true" disabled="#{not checkbox.value}" />
    <h:inputText required="true" disabled="#{not checkbox.value}" />
    <h:inputText required="true" disabled="#{not checkbox.value}" />

    <h:commandButton value="submit" />
    <h:messages />
</h:form>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • the question is more about why it shows validation massage when form is disabled, but not about how to bind check-box with a model. – ses Aug 28 '13 at 21:03
  • Because it's not disabled during apply request values phase. Did you read and understood the answer? Did you try the given kickoff example? – BalusC Aug 28 '13 at 22:38
  • Then there's something more into the code causing this, which is not visible in the information provided so far. It works at least for me in a completely blank playground project with Mojarra 2.1.25 and exactly the above code with `action="#{bean.save}"` in button. – BalusC Aug 29 '13 at 14:57
  • Maybe be I should include "" to be updated while do render on the panel? because my "messages" are out of the panel. – ses Aug 29 '13 at 15:00
  • That can impossibly prevent the action method from being invoked. Did you really not create an SSCCE on your behalf...?? How would you ever be able to properly demonstrate the problem to others while not being able to understand and explain the problem in technical terms alone? – BalusC Aug 29 '13 at 15:07
  • I will create short example, maybe you are right (I hoped it is known issue). It seems you are only one guy in internet who knows jsf. There is your answer: http://stackoverflow.com/questions/8449838/jsf-ajax-validation-execute-this-render-form-inconsistent-depending-on-pr I wonder maybe it is related to my issue. – ses Aug 29 '13 at 15:17
  • you are right - simple app from scratch works well, as expected. but not our real app. it seem there is kind of bug in custom validator somewhere – ses Aug 29 '13 at 17:12
  • And this is where creating an SSCCE comes to rescue. Basically, create a copy of offending code. Eliminate/merge as many as possible code, classes, methods, tags, attributes, etc..etc.. until you end up with smallest possible snippet which exhibits the problem by just copy'n'paste'n'running and post it unmodified in the question. Any experienced developer will spot the problem in no-time and give you the solution. Or, perhaps, you've already nailed down the cause yourself during the procedure of creating the SSCCE! – BalusC Aug 29 '13 at 17:15