Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 - Ajax submit validation on inputs

Tags:

jsf

jsf-2

I have a form that needs to be submitted with ajax. I am trying to get the validation to work but it wont work when I use ajax. When I take the ajax out and submit the form with an empty testinput it properly triggers he validation and does not submit the form. How can I do this with an ajax call. My form is below.

<h:form>
  <f:ajax>
   <h:panelGrid columns="3">
       <h:outputLable for=test" value="testinput:" />
       <h:inputText id=test" required="true" label="testinput" />
       <h:message for="test" id="testError"/>
   </h:panelGrid>

   <h:commandButton value="Submit" actionListener="#{testBean.doSomething}" />
  </f:ajax>
</h:form>
like image 297
JsfNewbie Avatar asked Feb 26 '23 10:02

JsfNewbie


2 Answers

The <f:ajax> by default doesn't re-render the form, so you won't see anything. Replace <f:ajax> by <f:ajax execute="@form" render="@form" />.

like image 57
BalusC Avatar answered Mar 10 '23 23:03

BalusC


Maybe try something like this:

<h:form id="yourFormId">
  <!-- form content -->

  <h:commandButton value="Submit">
    <f:ajax execute="yourFormId" listener="#{testBean.doSomething}"/>
  </h:commandButton>
</h:form>
like image 28
amorfis Avatar answered Mar 10 '23 22:03

amorfis