0

I want the Submit button to be enabled when the T&C checkbox is checked. Though my written logic is working alright the action of h:commandButton is not getting invoked even after it is enabled after checking the checkbox. Initially the button is disabled. The code works fine if I remove the disabled="TRUE" attribute but it doesn't serve the purpose. Here is my code :

<script type="text/javascript">
$(document).ready(start);
function start() {

    $('.testing').click(
            function() {
                if ($(document.getElementById('form2:check'))
                        .is(':checked')) {
                    $(document.getElementById('form2:saveForm'))
                            .removeAttr('disabled');
                } else {
                    $(document.getElementById('form2:saveForm')).attr(
                            'disabled', 'disabled');
                }
            });
                 }
</script>

JSP Code:

<h:form id="form2">
    <div class="testing">
        <h:selectBooleanCheckbox id="check" />
                Agree           
            <li class="buttons ">
                <div class="center">
                    <h:commandButton id="saveForm" styleClass="btTxt submit"
                        type="submit" value="Submit"
                        action="#{declarationFormBean.formSubmit}" disabled="true"></h:commandButton>
                </div>
            </li>
</h:form>

Please help.

Tanuj
  • 71
  • 2
  • 13

1 Answers1

2

That's because you're solely removing the disabled attribute in the JSF-generated HTML representation using JavaScript/jQuery. This doesn't change anything to the disabled attribute in the actual JSF component which is consulted during decoding the action event.

This is actually a Good ThingTM of JSF, because otherwise any hacker would be able to bypass any JSF disabled, readonly and rendered attribute by just manipulating the HTML DOM tree while they are initially used to block them like so rendered="#{request.isUserInRole('ADMIN')}".

You need to enable the button by JSF instead of JS/jQuery.

<h:selectBooleanCheckbox binding="#{check}">
    <f:ajax render="saveForm" />
</h:selectBooleanCheckbox>
<h:commandButton id="saveForm" ... disabled="#{not check.value}" />

That's all. No custom JS/jQuery mess necessary, nor any additional JSF bean properties. The above code is complete as-is.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your answer but i already tried this given by you in a similar question. It shows me Unknown tag. In the project proerties -> Project Facets I can see the JavaServer Faces version as 2.2 then why this problem? – Tanuj Nov 19 '13 at 13:45
  • Are you using JSP instead of Facelets (XHTML) or so? – BalusC Nov 19 '13 at 13:45
  • 1
    You're not supposed to use JSP anymore. It is **deprecated** and succeeded by Facelets 4 years ago. You need to use Facelets instead. New JSF 2.0 tags like `` are available in Facelets only, not in JSP. It's actually quite surprising to see someone using JSF 2.2 on JSP. No one sane tutorial shows that. Please make sure that you're learning JSF by recent JSF 2.x targeted resources, not by outdated JSF 1.x resources. So many things are done differently (read: better!) in JSF 2.x as compared to JSF 1.x. – BalusC Nov 19 '13 at 13:47
  • Anyway of doing it in JSP only? The rest of the application works fine and I am doing this just to improve the user experience. – Tanuj Nov 19 '13 at 13:55
  • 1
    One way would be using `onclick="submit()"` instead of ``. Another way would be continuning with this custom JS/jQuery mess but then add a hidden input in order to instruct JSF to re-enable the button. Even though it works, there's absolutely no point of continuing using deprecated technology. It's high time to migrate. – BalusC Nov 19 '13 at 13:57
  • I am still stuck on it. The onclick="submit()" doesn't work on and it doesn't get invoked when I use the action attribute. Please tell some work around or demo code. Sorry for troubling you. – Tanuj Nov 20 '13 at 09:44
  • In the checkbox, not in the command button. The `` was originally also in the checkbox. – BalusC Nov 20 '13 at 09:44
  • I get a JspPropertyNotFoundException on using onclick in checkbox. Property 'formSubmit' not found on type DeclarationFormBean – Tanuj Nov 20 '13 at 09:55
  • No, just `onclick="submit()"`. Don't modify it. I have never said to use `onclick="#{declarationFormBean.formSubmit}"`. Just keep it in the `action` of the command button as you originally have. From my whole answer which is targeted on your original code you just have to replace `` by `onclick="submit()"`. – BalusC Nov 20 '13 at 09:56
  • Pheww. It finally worked. Thank you so much for your patience and quick response. :-) – Tanuj Nov 20 '13 at 10:10