4

I asked my question here but I think it lacks some obvious information. Hence I'm posting my question again.

I have a JSF form which has some required validation on h:inputText fields. When submitting the form I want to skip(or ignore) the required validation check and still invoke the application. I do want to do the form data validation and show the errors based on some condition.

Basically I want to skip the validations and invoke the application when the form is submitted using an ajax call and do the validation when the form is submitted via clicking a submit button.

Community
  • 1
  • 1
Srini Kandula
  • 981
  • 2
  • 18
  • 47

1 Answers1

11

Just put that condition straight in the required attribute.

<h:inputText ... required="#{someCondition}" />

It namely just accepts any EL expression like as many other attributes. Many starters think that you can only hardcode a "true" or "false" string in it. This is untrue.

For example, when you want to let it evaluate true only when the save button is actually pressed:

<h:inputText ... required="#{not empty param[save.clientId]}" />
...
<h:commandButton value="Cancel" ... />
<h:commandButton binding="#{save}" value="Save" ... />

(note: code is complete as-is, you do not need to bind it to a bean property)

This way the required attribute only evaluates true when the save button is pressed and it evaluates false for any other button or ajax event listener.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This actually sounded like a brilliant idea. However it's not working as expected in my application. Basically the required validation is not being triggered. Is there a way to debug if the condition is being evaluated to true on the server side when doing the validation? What I did was to map the required to a backing bean property i.e. required="#{validate.validateRequired}" and then have a hidden form field mapping to that property which will be turned on when clicking the button to submit the form. My forms are designed in a uncommon fashion hence I needed to do this. – Srini Kandula Oct 31 '13 at 19:40
  • It should work. Perhaps you're using JSF 1.x without explicitly mentioning it in the question? In JSF 1.x, the `UIComponent` class doesn't have an argumentless `getClientId()` method and hence `#{save.clientId}` would fail. JSF 2.x was released almost 4 years ago, no one would expect that one would still fiddle with JSF 1.x these days. If you're using an older version of a library than currently available, you should **always** explicitly mention it in the question. For JSF 1.x there's also a way, but you'd have to hardcode the component's client ID yourself. – BalusC Oct 31 '13 at 19:42
  • I'm using JSF2.x btw. Sorry I had to edit my comment while you answer to it. – Srini Kandula Oct 31 '13 at 19:48
  • Assuming that the hidden field's value can only be `true` or `false`, then just check that hidden field value in the `required` attribute. E.g. ``. – BalusC Oct 31 '13 at 19:51
  • In the HTML(using firebug) I do see that the hidden field is set to false until I click on this button to submit the form via ajax. Before triggering the ajax call to submit the form with validations turned on I set the value to true in javascript (which also confirmed via inspecting using firebug) – Srini Kandula Oct 31 '13 at 19:56
  • I also set a breakpoint in the setter method for the property and see true is coming indeed when my intended ajax call is triggered. – Srini Kandula Oct 31 '13 at 19:57
  • BTW if it's not explicit, BalusC's suggestion did work for me. Thank you BalusC :). – Srini Kandula Nov 05 '13 at 01:27
  • @BalusC this works for me. If I have a f:validateLongRange in my input field, can I do the same to bypass the validation? I tried to set disabled flag of f:validateLongRange the same way as you mentioned in your post. It didn't work. Can you plz help? – Ani Oct 01 '14 at 03:05
  • 1
    This is a trick. How about if the require fields are marked as "*", this makes the "*" disappeared after ajax. Actually, the ideal way should let the ajax control doesn't do the validation at all. – ivenxu Jul 15 '16 at 04:49
  • The InputGroup feature works pretty well if you use Primefaces. I'm using v. 6.2. – nettie Jan 14 '22 at 18:03