Disable/Enable h:commandButton on validation fails/ is ok

痞子三分冷 提交于 2019-12-07 23:49:18

问题


I got a TextBox with a validator and a commandbutton. When the validation of the textbox fails I want to disable the commandbutton otherwise it should be enabled. I don't want to use any codebehind so it should be working without any helper properties in the bean.

So how can I tell the commandbutton to be disabled/enabled depending on the validator state?

  <h:outputLabel for="category" value="#{msg['category.create']}"/>
                    <h:inputText id="category" required="true"  label="#{msg['category.create']}" value="#{CreateCategoryBean.newCategory.name}" >
                        <f:validator validatorId="Get.Validator.CategoryValidator" />
                        <f:ajax event="keyup" render="categorymessage"  />
                    </h:inputText>

                    <h:messages for="category" id="categorymessage"/>

                    <h:commandButton id="submit" value="#{msg['default.create']}" action="#{CreateCategoryBean.CreateCategory()}"

回答1:


As you've already a <f:ajax event="keyup">, just let it update the <h:commandButton> as well wherein you in turn in its disabled attribtue check if UIComponent#isValid() of <h:inputText> doesn't return false.

<h:inputText binding="#{category}" ...>
    <f:ajax ... render="categoryMessage submit" />
</h:inputText>

<h:commandButton id="submit" ... disabled="#{not category.valid}" />

No additional bean properties necessary. The above code is complete as-is.

See also:

  • How does the 'binding' attribute work in JSF? When and how should it be used?


来源:https://stackoverflow.com/questions/17926245/disable-enable-hcommandbutton-on-validation-fails-is-ok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!