2

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()}"
Briefkasten
  • 1,964
  • 2
  • 25
  • 53

1 Answers1

3

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555