1

I have the following code snippet and the standard client side html validation is not triggerd. How can I trigger the standard validation and get the action called if valid.

<h:form>
<h:inputText type="text" value="#{myBean.value}">
<f:passThroughAttribute name="required" value="required" />
</h:inputText>
<h:commandButton value="submit" type="submit" action="#{myBean.save}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:from>

Without the ajax the client side validation will be triggered and the form will not be send if the input is empty.

How can I reach this with ajax?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
flosk8
  • 465
  • 1
  • 6
  • 17
  • I don't see any client-side validation. Adding Ajax functionality might override the client side validation, depending on how you did it. Show us the code! – noone Aug 28 '13 at 06:00
  • @noone: it's time for you to dive into HTML5: http://www.wufoo.com/html5/attributes/09-required.html and http://diveintohtml5.info/forms.html In the future better try to recreate OP's problem based on information provided so far before posting comments based on uncertainty. – BalusC Aug 28 '13 at 10:51
  • @BalusC Thanks for the links. I should have thought more about that `f:passThroughAttribute`, which seemed weird to me. :) – noone Aug 28 '13 at 12:02

1 Answers1

3

JSF ajax engine does not check HTML5's form.checkValidity() before proceeding the request. Essentially, you need to do that job yourself in the onclick attribute of the command button. If it returns false, then it will block the ajax action.

Please note that you should never rely on only client side validation, because that's fully controllable by the enduser. I strongly recommend to add server side validation as well. When you add ajax, you can easily make use of server side validation with the same user experience as client side validation.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • If I use the commandButton with enabled partialsubmit and update only a specific form, still the page gets submited after the validation. Is there a way to avoid submitting the page? – leostiw Nov 04 '15 at 11:11
  • I assumed it was related to the topic. Posted a new quesiton, would appreciat if you'd take a look: http://stackoverflow.com/questions/33520616/avoid-submitting-whole-page-after-html5-validation – leostiw Nov 04 '15 at 11:26